取消列出R中的多个元素的列表

我已经从twitter中提取了数据,它显示为List列(运行代码后会得到什么)。我希望输出显示为“损坏”列中的内容。

Image for data

data <- data.frame(matrix(, nrow=4, ncol=2))
colnames(data)[1:2] <- c("List", "Broken")
data$List[1] <- 1
data$List[2] <- list(c("1", "SmythsToysUK"))
data$List[3] <- list(c("1", "FortniteGame", "CityCtrMirdif", "itpliveme"))
data$List[4] <- 1
data$Broken[1:4]<- c("SmythsToysUK","FortniteGame","CityCtrMirdif","itpliveme")
评论
菊Oo
菊Oo

We can remove all the numbers from List column.

temp <- unlist(data$List)
data$Broken <- temp[is.na(as.numeric(temp))]
data

#                                       List        Broken
#1                                         1  SmythsToysUK
#2                           1, SmythsToysUK  FortniteGame
#3 1, FortniteGame, CityCtrMirdif, itpliveme CityCtrMirdif
#4                                         1     itpliveme
点赞
评论
Eliv
Eliv

We can use grep with unlist. After unlisting the list, select only the elements that have letters

data$Broken <- grep("[A-Za-z]", unlist(data$List), value = TRUE)
data$Broken
#[1] "SmythsToysUK"  "FortniteGame"  "CityCtrMirdif" "itpliveme"   

Or another option is to remove the first element which seems to be index and then unlist

unlist(sapply(data$List, `[`, -1))

注意:使用此选项,不会有任何警告

点赞
评论