我使用“rtweet”库下载了一些推文。它的 search_tweets() 函数创建一个列表(类型)对象,而它的类是“tbl_df”“tbl”“data.frame”。为了进一步处理它,我需要将此 search_tweets() 输出转换为数据帧。
comments <- search_tweets(
queryString, include_rts = FALSE,
n = 18000, type = "recent",
retryonratelimit = FALSE)
typeof(comments)
列表
class(comments)
“tbl_df” “tbl” “data.frame”
我尝试使用 as.data.frame() 将列表转换为数据框,但没有改变类型,我还尝试将其包装成 as.dataframe(matrix(unlist(comments))),但没有改变也输入
commentData <- data.frame(comments[,1])
for (column in c(2:ncol(comments))){
commentData <- cbind(commentData, comments[,column])
}
type(comments)
输出:列表
comments <- as.data.frame(comments)
输出:列表
这两个代码都没有改变类型,而是改变了类。我应该如何更改类型?因为,我想将这些推文存储到一个数据框中,然后将它们写为 csv (write_csv)。
当我将“评论”写入 csv 时,它会引发错误。
write_csv(comments, "comments.csv", append = TRUE)
错误:stream_delim_(df, path, ..., bom = bom, quote_escape = quote_escape) 中的错误:不知道如何处理列表类型的向量。
dput(comments)
dput(comments) structure(list(user_id = c("1213537010930970624", "770697053538091008", "39194086", "887369171603931137", "924786826870587392", "110154561", "110154561", "1110623370389782528", "1201410499788689408", "1208038347735805953 ", "15608380", "54892886", "389914405", "432597210", "1196039261125918720" ), status_id = c("1217424480366026753", "1217197024405143552", "1217057752918392832", "1217022975108616193", "1217002616757997568", "1216987196714094592" , “1216986705170923520”, “1216978052472688640”, “1216947780129710080”, “1216943924796739585”, “1216925375789330432”, "1216925016605880320", "1216924608944734208", "1216921598294249472", "1214991714688987136"), created_at = structure(c(1579091589, 1579037359, 1579004154, 1578995863, 1578991009, 1578987332, 1578987215, 1578985152, 1578977935, 1578977016, 1578972593, 1578972507, 1578972410, 1578971693, 1578511572), class = c("POSIXct", "POSIXt"), tzone = "UTC"), screen_name = c("SufferMario", "_Mohammadtausif", "avi_rules16", "Deb05810220", "SriPappumaharaj", " Poison435”、“Poison435”、“RajeshK38457619”、“KK77979342”、“beingskysharma”、“tetisheri”、“sohinichat”、“nehadixit123”、“panwarsudhir1”、“NisarMewati1"),
desired output in csv