我有这样的数据集
Id Comments
1 ,,,,A,,,,,B,,,,,,C
2 ,,,,,,A,,,,,,,C,,,
3
4 ,,,,,,,,,,Z,,,,,,,,
我想要制作的是像这样的输出
Id Feedback Comments
1 Yes A,B,C
2 Yes A,C
3 NA
4 Yes Z
需要帮助。
-----------------------实际数据的子集-------------------- -----
t9 = structure(list(ID = c(242938L, 309790L, 339402L), Description = c("",
" , , , , , , , , , , , A, , , , , , , , , , , B, , , , , , C, , , , D, , , , , , , , , , E, , , , , , , , , , , F",
" , , A, , , D, , , , , , , , , S, , , , , , , D")), .Names = c("ID", "Comments"), row.names = c(NA, 3L), class = "data.frame")
答案 0 :(得分:1)
您可以使用gsub
删除不需要的“,”,根据“评论”中的元素是否为空字符串(nzchar
)创建新列(“反馈”)。 / p>
df1$Comments <- gsub(',+', ',', gsub('^,+|,+$', '', df1$Comments))
df1$Feedback <- ifelse(nzchar(df1$Comments), 'Yes', NA)
df1
# Id Comments Feedback
#1 1 A,B,C Yes
#2 2 A,C Yes
#3 3 <NA>
#4 4 Z Yes
或者您可以更改“评论”列
library(stringr)
df1$Comments <- sapply(str_extract_all(df1$Comments, '[A-Za-z]+'), toString)
使用t9
数据,删除“空格”并使用上面的代码
t9$Comments <- gsub(' ', '', t9$Comments)
(t9$Comments <- gsub(',+', ',', gsub('^,+|,+$', '', t9$Comments)))
#[1] "" "A,B,C,D,E,F" "A,D,S,D"
df1 <- structure(list(Id = 1:4, Comments = c(",,,,A,,,,,B,,,,,,C",
",,,,,,A,,,,,,,C,,,",
"", ",,,,,,,,,,Z,,,,,,,,")), .Names = c("Id", "Comments"),
class = "data.frame", row.names = c(NA, -4L))