我在数据框中有一列,每个单元格中有多个单词,用";"分隔(第二栏).
my_dataframe <- data.frame( first_column = c("x", "y", "x", "x", "y"),
second_column = c("important; very important; not important",
"not important; important; very important",
"very important; important",
"important; not important",
"not important"))
> my_dataframe
first_column second_column
1 x important; very important; not important
2 y not important; important; very important
3 x very important; important
4 x important; not important
5 y not important
我想每个单元格保留一个单词:最重要的一个.
所以我按优先顺序列了一个单词列表:
reference_importance <- list("very important", "important", "not important")
我想在第二栏中看到:
second_column
1 very important
2 very important
3 very important
4 important
5 not important
我试过了
for (i in 1:dim(my_dataframe)[1]) {
for (j in 1:length(reference_importance)) {
if (j %in% my_dataframe$second_column){
my_dataframe$second_column[i] <- paste(j)
break}
}
}
Then I thought the problem was that it didn't consider the different words separated by ";" so 我试过了 this:
for (i in 1:dim(my_dataframe)[1]) {
value_as_list <- strsplit(my_dataframe$second_column[i], ";")
print(value_as_list)
for (j in reference_importance) {
if (j %in% value_as_list){
my_dataframe$second_column[i] == j
break}
}
}
但这些并没有改变我专栏中的任何内容...
(我做这个例子是为了简化,但实际上我有一个巨大的表格,里面有更多的单词和可能性.这就是为什么我try 使用循环来完成它,而不是手动分配可能的答案.)