pinky
pinky

Reputation: 11

Renaming a categorical variable based on values of other variable in R

I have a sample dataset 'a' like this:

> a
  group marks  upd class
1     T     2   up     A
2     C     3   up  <NA>
3     C     4 down     B
4     T     5   up  <NA>
5     T     6 down     D
6     C     7   up  <NA>
7     T     1 down  <NA>
8     T     0 down     G

Here for each group (T or C) there are records in 'class' variable with values and null. Now for each group where ever the class is null ,I want that group to be renamed as T-NULL or C-NULL respectively. If the group (T or c) has some value in 'class' ,the group name should be as it is. How can we write a code in R for this?

Upvotes: 0

Views: 700

Answers (2)

akrun
akrun

Reputation: 887148

We can use data.table. Convert the 'data.frame' to 'data.table', based on the logical condition for NA values in 'class', we paste the NULL string to 'group' column and assign (:=) the modified string to 'group'

library(data.table)
setDT(a)[is.na(class), group := paste(group, "NULL", sep="-")]
a
#    group marks  upd class
#1:      T     2   up     A
#2: C-NULL     3   up    NA
#3:      C     4 down     B
#4: T-NULL     5   up    NA
#5:      T     6 down     D
#6: C-NULL     7   up    NA
#7: T-NULL     1 down    NA
#8:      T     0 down     G

Or using base R

a$group[is.na(a$class)] <- paste(a$group[is.na(a$class)], "NULL", sep="-")

NOTE: We are assuming that the 'group' column is character class. If it is factor class, convert to character class (as.character can be used or when reading the dataset use stringsAsFactors=FALSE) or add both the 'C-NULL' and 'T-NULL' as levels to factor and then assign the modified string.

Upvotes: 1

Xiongbing Jin
Xiongbing Jin

Reputation: 12097

Try this

a$group = paste0(a$group, ifelse(is.na(a$class), "-NULL", ""))

Upvotes: 1

Related Questions