如何在R数据帧中找到正值和负值的按组数目?
要查找R数据帧中正值和负值的按组数目,可以使用dplyr包的mutate函数。例如,如果我们有一个名为df的数据框,其中有一个分类列x和一个数字列y,则可以使用以下命令找到分类列的正值和负值的数量-
df%>%group_by(x)%>%mutate(positive=sum(y>0),negative=sum(y<0))
考虑以下数据帧-
示例
Group<-sample(LETTERS[1:4],20,replace=TRUE)输出结果y1<-rnorm(20)
df1<-data.frame(Group,y1)
df1
Group y11 C 0.85135509
2 D -1.65551392
3 C -3.02201905
4 B -0.89625797
5 A 0.28129292
6 A -0.04801872
7 D -0.95801637
8 B 1.42274072
9 A -1.18959023
10 A 0.60031795
11 C 0.22474189
12 B 2.17627556
13 D 1.24592142
14 A -0.46659749
15 B 0.43163726
16 D -0.95450596
17 C 2.89909436
18 D 0.55951749
19 C -0.48761069
20 B 0.85106755
加载dplyr程序包并找到df1中“组”列的正负值的按组数目-
示例
library(dplyr)df1%>%group_by(Group)%>%mutate(positive=sum(y1>0),negative=sum(y1<0))
小标题:20 x 4
群组:群组[4]
输出结果
Group y1 positive negative<chr> <dbl> <int> <int>
1 C 0.851 3 2
2 D -1.66 2 3
3 C -3.02 3 2
4 B -0.896 4 1
5 A 0.281 2 3
6 A -0.0480 2 3
7 D -0.958 2 3
8 B 1.42 4 1
9 A -1.19 2 3
10 A 0.600 2 3
11 C 0.225 3 2
12 B 2.18 4 1
13 D 1.25 2 3
14 A -0.467 2 3
15 B 0.432 4 1
16 D -0.955 2 3
17 C 2.90 3 2
18 D 0.560 2 3
19 C -0.488 3 2
20 B 0.851 4 1
示例
Class<-sample(c("Male","Female"),20,replace=TRUE)输出结果y2<-rnorm(20)
df2<-data.frame(Class,y2)
df2
Class y21 Male 0.05278402
2 Female 1.61835971
3 Female 1.17452205
4 Male -0.61445588
5 Female -0.25727985
6 Female 0.75821980
7 Male -1.08398301
8 Male 0.77650504
9 Male 0.54157874
10 Male 0.42124703
11 Male 0.36434790
12 Male -1.03566231
13 Male 0.06208824
14 Male 0.11831360
15 Male 1.22460403
16 Male -2.43157148
17 Male -0.50869299
18 Male -1.14549469
19 Female 1.45219635
20 Male -1.14271790
在df2中找到Class列的正负值的按组数目-
示例
df2%>%group_by(Class)%>%mutate(positive=sum(y2>0),negative=sum(y2<0))输出结果# A tibble: 20 x 4
# Groups: Class [2]
Class y2 positive negative<chr> <dbl> <int> <int>
1 Male 0.0528 8 7
2 Female 1.62 4 1
3 Female 1.17 4 1
4 Male -0.614 8 7
5 Female -0.257 4 1
6 Female 0.758 4 1
7 Male -1.08 8 7
8 Male 0.777 8 7
9 Male 0.542 8 7
10 Male 0.421 8 7
11 Male 0.364 8 7
12 Male -1.04 8 7
13 Male 0.0621 8 7
14 Male 0.118 8 7
15 Male 1.22 8 7
16 Male -2.43 8 7
17 Male -0.509 8 7
18 Male -1.15 8 7
19 Female 1.45 4 1
20 Male -1.14 8 7
以上是 如何在R数据帧中找到正值和负值的按组数目? 的全部内容, 来源链接: utcz.com/z/320036.html