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
  • 1
  • 16
  • 47
  • 69
Angela Ju
  • 17
  • 3
  • Try `data[,mn] <- lapply(data[,mn], function(x) factor(x, levels=2:3, labels=c('Male', 'Female'))` – akrun Aug 12 '15 at 20:43
  • Can you give an example of the data before and after the desired transformation? – user295691 Aug 12 '15 at 20:43
  • [This](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) should help you in making reproducible example – akrun Aug 13 '15 at 14:03

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
  • 874,273
  • 37
  • 540
  • 662
  • 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 `[<-.data.frame`(`*tmp*`, , mn, value = list(1L, 1L, 1L, 1L, : provided 516 variables to replace 1 variables – 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]<-factor(data[,mn],levels=c(2,3),labels=c("Male","Female")) – 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