[英]how change a list class variable into string in r
I am very new to R and I am so interested in learning this wonderful program for data analysis. 我对R非常陌生,对学习这个出色的数据分析程序非常感兴趣。
I was working on a worksheet which was given by my teacher on tweets data. 我正在研究一个工作表,该工作表由我的老师根据推文数据给出。 I found one task so difficult and I could get passed into the next question as I need the data ready by order.
我发现一项任务如此困难,由于需要按顺序准备好数据,因此我可以将其传递给下一个问题。 I would like to ask how to change a "list" class variable into "string" class.
我想问一下如何将“列表”类变量更改为“字符串”类。 The following is the first 6 elements of the variable:
以下是变量的前6个元素:
head(tweets.merged$hashtag_and_mentioned, 6)
[[1]]
character(0)
[[2]]
[1] "#Aufbruch." " @MartinSchulz:" " #Deutschland"
[[3]]
[1] "#zeitfuermartin" " @MartinSchulz."
[[4]]
[1] "#zeitfuermartin" " @MartinSchulz"
[[5]]
character(0)
[[6]]
character(0)
I want to change them into some thing like: 我想将它们更改为以下内容:
[[1]]
NA or 0
[[2]]
[1] "Aufbruch. @MartinSchulz: #Deutschland"
Do: 做:
l <- tweets.merged$hashtag_and_mentioned
unlist(lapply(l, function(x) if (length(x) == 0) {NA} else {paste(x, collapse = "")})) # or change NA to 0
But if you allow character(0)
to become "", it is even easier: 但是,如果允许
character(0)
变成“”,则更加简单:
unlist(lapply(l, function(el) paste(el, collapse = ""))
Or even shorter: 甚至更短:
unlist(lapply(l, paste, collapse = ""))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.