R 如何找出x行中的某个模式与另一行中的某个值对应的次数?
我想知道第1列、第2列和第3列中的某个模式与第4列(类)中的某个值对应多少次。我的data.frame如下所示:R 如何找出x行中的某个模式与另一行中的某个值对应的次数?,r,dataframe,pattern-matching,R,Dataframe,Pattern Matching,我想知道第1列、第2列和第3列中的某个模式与第4列(类)中的某个值对应多少次。我的data.frame如下所示: one <- c(-1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1) two <- c(0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1) three <- c(0, 0, 0, 0, -1, 0, 0, 0,
one <- c(-1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1)
two <- c(0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1)
three <- c(0, 0, 0, 0, -1, 0, 0, 0, 0, -1, -1, 0, -1, -1, 0, 0, 0, -1, -1, 0)
class <- c(0, 1, 1, 0, -1, -1, 1, 0, 1, -1, 1, 0, -1, -1, 1, 0, 1, -1, -1, 1)
mydf <- data.frame(one, two, three, class)
mydf
one two three class
1 -1 0 0 0
2 1 1 0 1
3 1 1 0 1
4 -1 0 0 0
5 -1 0 -1 -1
6 1 1 0 -1
7 1 1 0 1
8 1 1 0 0
9 1 1 0 1
10 -1 0 -1 -1
11 -1 0 -1 1
12 -1 0 0 0
13 -1 0 -1 -1
14 -1 0 -1 -1
15 1 1 0 1
16 1 1 0 0
17 1 1 0 1
18 -1 0 -1 -1
19 -1 0 -1 -1
20 1 1 0 1
# column one contains only value 1 or -1
# column two contains only value 1 and 0
# column three contains only values 0 and -1
# column class contains all values 1, 0 and -1
我想知道在最后一列(类)中,每个模式对应于1、0和-1的次数。
我该怎么做??我在想,如果我有字符而不是数字(例如,1=a,0=b,-1=c),我可以将一二三列合并成一个包含特定术语的单列(例如,abc,acb,bac,bca,…)。然后我可以找出术语abc在第四列中对应于1、0和-1的次数。甚至可以将一列合并到四列,并计算包含结果项的行数(abca、abcb、abcc、acba、acbb等)
如果有人知道一种直接(更优雅)的方法,我会很高兴!
非常感谢
编辑/新建任务:
# with your answers i get:
x <- do.call(paste, expand.grid(lapply(mydf[-4], unique)))
## Paste together the first three columns
y <- do.call(paste, mydf[-4])
## Tabulate
x <- factor(x)
table1 <- table(pattern = x[match(y, x)], value = mydf[, 4])
table1
value
pattern -1 0 1
-1 0 -1 6 0 1
-1 0 0 0 3 0
-1 1 -1 0 0 0
-1 1 0 0 0 0
1 0 -1 0 0 0
1 0 0 0 0 0
1 1 -1 0 0 0
1 1 0 1 2 7
(本例中有一些模式没有出现。在本例中,第四列中应该有一个0)
有人知道怎么做吗?谢谢 以下是我对你所问问题的解释:
## Create the combinations that are possible
x <- do.call(paste,
expand.grid(lapply(mydf[-4], unique)))
## Paste together the first three columns
y <- do.call(paste, mydf[-4])
## Tabulate
table(pattern = x[match(y, x)], value = mydf[, 4])
# value
# pattern -1 0 1
# -1 0 0 0 3 0
# -1 0 -1 6 0 1
# 1 1 0 1 2 7
这里有一些方法。他们使用问题代码中构造的
mydf
(不同于所显示的mydf
)。数据中显示的每个模式和类组合都有一行,最后一列显示了这些组合的数量
1)合计
aggregate(count ~., cbind(count = 1, mydf), length)
library(data.table)
DT <- data.table(mydf, key = "class,one,two,three")
DT[, list(count = .N), by = key(DT)]
class one two three count
1: -1 -1 -1 -1 6
2: -1 1 1 0 1
3: 0 -1 -1 0 3
4: 0 1 1 0 2
5: 1 -1 -1 -1 1
6: 1 1 1 0 7
给予:
one two three class count
1 -1 -1 -1 -1 6
2 1 1 0 -1 1
3 -1 -1 0 0 3
4 1 1 0 0 2
5 -1 -1 -1 1 1
6 1 1 0 1 7
one two three class count(*)
1 -1 -1 -1 -1 6
2 1 1 0 -1 1
3 -1 -1 0 0 3
4 1 1 0 0 2
5 -1 -1 -1 1 1
6 1 1 0 1 7
2)sqldf
library(sqldf)
sqldf("select one, two, three, class, count(*)
from mydf
group by class, one, two, three")
给予:
one two three class count
1 -1 -1 -1 -1 6
2 1 1 0 -1 1
3 -1 -1 0 0 3
4 1 1 0 0 2
5 -1 -1 -1 1 1
6 1 1 0 1 7
one two three class count(*)
1 -1 -1 -1 -1 6
2 1 1 0 -1 1
3 -1 -1 0 0 3
4 1 1 0 0 2
5 -1 -1 -1 1 1
6 1 1 0 1 7
3)数据表
aggregate(count ~., cbind(count = 1, mydf), length)
library(data.table)
DT <- data.table(mydf, key = "class,one,two,three")
DT[, list(count = .N), by = key(DT)]
class one two three count
1: -1 -1 -1 -1 6
2: -1 1 1 0 1
3: 0 -1 -1 0 3
4: 0 1 1 0 2
5: 1 -1 -1 -1 1
6: 1 1 1 0 7
添加了聚合、data.table、reforme2。也许,类似于
表(do.call(粘贴,mydf[-4])、mydf[[4]])
,这实际上是您心目中的概念,可能会有所帮助?@大家:对不起,伙计们,我的英语。。我总是把列和行弄乱!对于其他的错误我很抱歉,我有点乱:)!现在应该没事了!是的,这看起来不错!!但是为什么我只能得到一张3x3的桌子呢?我应该得到一个包含8行(模式)和3列(值1,0,-1)的表@cptn,您必须使用因子来获得所有可能的级别。请参阅我的更新。+1。在我看到您的sqldf
方法之后,我打算建议aggregate
和“dplyr”(mydf%.%groupby(class,one,two,three)%。%summary(x=length(class))
),但我认为您已经涵盖了所有内容!
library(reshape2)
dcast(mydf, ... ~ class, fun = length)
Using class as value column: use value.var to override.
one two three -1 0 1
1 -1 -1 -1 6 0 1
2 -1 -1 0 0 3 0
3 1 1 0 1 2 7