【发布时间】:2015-12-14 12:27:19
【问题描述】:
下面有两个数据框。
set.seed(12345)
df1 <- data.frame(
y1 = sample(rep(c(0:1),length.out = 50)),
y2 = sample(rep(c(0:1),length.out = 50)),
y3 = sample(rep(c(0:1),length.out = 50)),
y4 = sample(rep(c(0:1),length.out = 50)),
y5 = sample(rep(c(0:1),length.out = 50)),
y6 = sample(rep(c(0:1),length.out = 50))
)
df2 <- data.frame(x = c("y1","y2","y1:y2","y2:y3:y4","y5","y6"))
我想合并这两个数据框,但合并的结果将显示每个元素的“1”计数。我的另一个问题是,在第二个数据框中,某些列有多个元素,由“:”分隔。这将使我很难自动执行此操作。下面是我要实现的表
x count
1 y1 25
2 y2 25
3 y1:y2 11
4 y2:y3:y4 8
5 y5 25
6 y6 25
【问题讨论】:
-
y1:y2计数是什么意思? -
你尝试了什么?为什么它不起作用?
-
我认为基于
countfory1:y2,看来y2:y3:y4应该是4。