从逗号分隔的文本到矢量

时间:2019-03-12 15:42:30

标签: r

将数据结构转换为逗号分隔格式:

dframe = data.frame(id=c(1,2,43,53), title=c("text1,color","color,text2","text2","text3,text2"))

将其转换为布尔型向量,每行中都存在或不存在此预期输出:

dframe = data.frame(id=c(1,2,43,53), text1=c(1,0,0,0), color=c(1,1,0,0), text2=c(0,1,1,1), text3=c(0,0,0,1))

1 个答案:

答案 0 :(得分:1)

我们可以使用separate_rows中的spreadtidyverse

library(tidyverse)
dframe %>%
  separate_rows(title, sep = ",") %>%
  mutate(id2 = 1) %>%
  spread(title, id2, fill = 0)

输出:

# A tibble: 4 x 5
# Groups:   id [4]
     id color text1 text2 text3
  <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1     1     0     0
2     2     1     0     1     0
3    43     0     0     1     0
4    53     0     0     1     1