I have a column in a data frame with multiple words in each cell separated by ";" (second column).
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
I want to keep one word per cell: the most important one.
So I made a list of the words by order of priority:
reference_importance <- list("very important", "important", "not important")
What I would like to get as a second column:
second_column
1 very important
2 very important
3 very important
4 important
5 not important
I tried
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 I tried 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}
}
}
But these don't change anything in my column...
(I made this example to simplify, but in reality I have a huge table with many more words and possibilities. That's why I try to do it with a loop and I don't just assign the possible answers manually.)