1

I wanted to add value label to a variable in the data frame. However, the variable name is based on substring match result. For example, I tried to look for a variable whose name include string “Gender", so I used the code

mn<-grep("Gender",names(data),value=TRUE)

to locate the variable.

Then I wanted to add value label for that variable, I tried:

data$mn<-factor(data$mn,levels=c(2,3),labels=c("Male","Female"))

but it did not work. Could anybody help me to fix the problem? Many thanks

oguz ismail
  • 39,105
  • 12
  • 41
  • 62
Angela Ju
  • 17
  • 3

1 Answers1

1

If there are multiple variables that match with 'Gender' in the names, we can loop it with lapply.

 data[,mn] <- lapply(data[,mn], function(x)
             factor(x, levels=2:3, labels=c('Male', 'Female'))

If the 'mn' has a length of 1 i.e. only a single column is matched, we don't need a loop (as showed in the comments by @Angela Ju)

 data[,mn] <- factor(data[,mn],levels=c(2,3),labels=c("Male","Female"))
akrun
  • 789,025
  • 32
  • 460
  • 575
  • Thank you. I tried your method, but the result showed it labeled all values as males, no females, and there is a warning message Warning message: In `[ – Angela Ju Aug 13 '15 at 13:59
  • @AngelaJu It is better you update your post with an example dataset using `dput` i.e. `dput(droplevels(head(yourdataset)))` and the expected output based on that. I was coding it based on just your description. I would suggest 10 rows, 5 column examples – akrun Aug 13 '15 at 14:01
  • 1
    Hi, I found this code works data[,mn] – Angela Ju Aug 13 '15 at 14:33
  • @AngelJu Yes, it should work for a single column i.e. if there is a single element in 'mn'. – akrun Aug 13 '15 at 14:42