This should be pretty simple; however, I can't get it right. I got the followin list:
list <- list(c("sofa", "couch"),
c("fridge"),
c("desk", "table", "brown"),
c("window", "clean"),
c("speaker"))
Which I am trying to convert to the following format (as a dataframe)
table <- data.frame(word = c("sofa", "couch", "fridge", "desk", "table", "brown", "window", "clean", "speaker"),
label = c("sofa couch", "sofa couch",
"fridge",
"desk table brown", "desk table brown", "desk table brown",
"window clean",
"window clean",
"speaker"))
I created this loop which makes sense to me but it is not working. I can't figure out why or if this is the best way of doing it
table <- data.frame(word = character(), label = character(), stringsAsFactors = FALSE)
value <- 0
for(i in 1:length(list)){
for(j in 1:length(list[[i]])){
value <- c(list[[i]][j], str_c(list[[i]], collapse = " "))
table <- rbind(table, value)
}
}