I'm working with data that look like this:
label |
---|
a |
b |
c |
I have a key-value index I've created (with code like that below) to use in creating a new column based on this existing column.
values <- c("word1", "word2", "word3")
keys <- c("a", "b", "c")
index <- setNames(as.list(values), keys)
With this index made, I was expecting to be able to create a new column with a simple one line for loop, like below.
df$newcol <- for (x in df$label){index$x}
This code doesn't seem to produce any change in my dataframe at all, though. What am I not understanding here?
CodePudding user response:
I would use dplyr::recode
:
df$newcol <- dplyr::recode(df$label, !!!index)
Output:
> df
label newcol
1 a word1
2 b word2
3 c word3
>
CodePudding user response:
Try check for (x in df$label){print(index$x)}
.
It will return
NULL
NULL
NULL
Instead of $
inside function, try using []
to call some values.
And, make a new vector first then append it to df$newcol
will work.
newcol <- c()
for (x in df$label){
newcol <- c(newcol, index[x])
}
df$newcol <- newcol
df
label newcol
1 a word1
2 b word2
3 c word3
Or, little change to for
loop
for (x in 1:nrow(df)){
df$newcol[x] <- index[df$label[x]]
}
df
label newcol
1 a word1
2 b word2
3 c word3
CodePudding user response:
You may want to use a data frame rather than a list index. Then using match()
.
index <- data.frame(values, keys)
df$newcol <- index[match(df$label, index$keys), 'values']
df
# label newcol
# 1 a word1
# 2 b word2
# 3 c word3
# 4 a word1
# 5 b word2
# 6 c word3
Data:
df <- data.frame(label=letters[c(1:3, 1:3)])
index <- structure(list(values = c("word1", "word2", "word3"), keys = c("a",
"b", "c")), class = "data.frame", row.names = c(NA, -3L))