1

I currently access the "v" column of the "two" data frame dynamically and I'd like to turn it into a factor with levels from the "one" data frame. I am currently getting NAs when I use factor() on two[,variable] . Any idea how to get this to work?

one =data.frame(v=c("B","A"))
two = data.frame(v=c("A","B"))
variable = "v"
two[,variable] = factor(two[,variable] ,levels = one$v, ordered = TRUE)

   v
1 <NA>
2 <NA>
camille
  • 16,432
  • 18
  • 38
  • 60
user3022875
  • 8,598
  • 26
  • 103
  • 167
  • Have you looked through previous posts like https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names and https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el?noredirect=1&lq=1? It looks like you're having trouble with how to get access to a column – camille Aug 19 '19 at 21:59

1 Answers1

2

Here, 'one' is also a data.frame, so we need to extract the corresponding column from 'one'

two[, variable] <- factor(two[,variable] ,levels = one[, variable], ordered = TRUE)
two[, variable]
#[1] A B
#Levels: B < A

If it is a tibble, the two[, variable] would still be a tibble with one column. To extract column as a vector (as factor method work on vector) either we use $ or [[

two[[variable]] <- factor(two[[variable]], levels = one[[variable]], ordered = TRUE)

Which would also work in base R


NOTE: The OP first showed an example where levels = one. Now, it is changed to one$v. which would work if it is run again after creating the 'two' data.frame

akrun
  • 874,273
  • 37
  • 540
  • 662