I have a dataset which needs to be recoded using labels in another dataset. Recoding works when I create the labels dataframe in R ,but not when I read the same data from a csv file.
Data:
df <- data.frame(
gender=c(1,2,1,2),
condition=c(1,1,2,2)
)
Codes created in R (using this works):
codes <- data.frame(
gender_values= c("1", "2"),
gender_labels= c("male gender","female gender"),
condition_values = c("1", "2"),
condition_labels = c("exp condition 1", "exp condition 2")
)
This works:
df$gender <- dplyr::recode(df$gender, !!!codes$gender_labels, .default = "nothing")
> df
gender condition
1 male gender 1
2 female gender 1
3 male gender 2
4 female gender 2
Using the csv codes, the code does not work:
> dput(codes_csv)
structure(list(gender_values = "\"1\",\"2\"", gender_labels = "\"male gender\", \"female gender\"",
condition_values = "\"1\",\"2\"", condition_labels = "\"exp condition 1\", \"exp condition 2\""), class = "data.frame", row.names = c(NA,
-1L))
df$gender <- dplyr::recode(df$gender, !!!codes_csv$gender_labels, .default = "nothing")
> df
gender condition
1 "male gender", "female gender" 1
2 nothing 1
3 "male gender", "female gender" 2
4 nothing 2
How could I format the cells in my csv file for the recode to work?
CodePudding user response:
codes_csv <- structure(list(gender_values = "\"1\",\"2\"", gender_labels = "\"male gender\", \"female gender\"",
condition_values = "\"1\",\"2\"", condition_labels = "\"exp condition 1\", \"exp condition 2\""), class = "data.frame", row.names = c(NA,
-1L))
codes_csv2 <- as.data.frame(lapply(codes_csv, function(x)
unlist(read.delim(textConnection(x), quote = "\"",sep = ",",
strip.white = T, header = F))))
codes_csv2
#> gender_values gender_labels condition_values condition_labels
#> V1 1 male gender 1 exp condition 1
#> V2 2 female gender 2 exp condition 2