同意评论-您需要创建一个可复制的示例。Excel中的任何内容都可以在R中做得更好(好了,几乎所有东西)有很多可能的方法-尝试按以下方式分组
df = data.frame(matrix(rnorm(40), nrow=10))
X0 <-sample(c(rep("A",3),rep("B",3),rep("C",4)), 10)
df$X0 <- x0
df # this is a simulated dataset
X1 X2 X3 X4 X0
1 -0.1203975 0.40176671 -0.1441914 -1.27356811 B
2 -1.2945289 0.34813784 0.3233932 0.80881055 B
3 1.0095911 0.69446583 -0.4377096 -1.41026705 A
4 -0.2547187 1.04914805 -0.8120526 -0.58734365 C
5 -0.1732585 -0.16422459 -0.8195833 0.44932410 A
6 0.5599036 -0.79013084 -0.9540998 -0.03861297 C
7 -0.5338999 0.03481498 0.3663478 0.44230402 B
8 0.9837636 0.98619889 -0.4901891 0.45207061 C
9 0.7892718 -0.23403087 -0.1102857 0.79010878 A
10 0.9392468 0.54867442 -0.4180297 -0.40482425 C
# Next, we count/index by X0 ~ you will need to do this by "site" /your choice
library(dplyr)
df1 <- df %>%
group_by(X0) %>%
add_count
df1
# A tibble: 10 x 6
# Groups: X0 [3]
X1 X2 X3 X4 X0 n
<dbl> <dbl> <dbl> <dbl> <chr> <int>
1 -0.120 0.402 -0.144 -1.27 B 3
2 -1.29 0.348 0.323 0.809 B 3
3 1.01 0.694 -0.438 -1.41 A 3
4 -0.255 1.05 -0.812 -0.587 C 4
5 -0.173 -0.164 -0.820 0.449 A 3
6 0.560 -0.790 -0.954 -0.0386 C 4
7 -0.534 0.0348 0.366 0.442 B 3
8 0.984 0.986 -0.490 0.452 C 4
9 0.789 -0.234 -0.110 0.790 A 3
10 0.939 0.549 -0.418 -0.405 C 4
# After the above, you can sort and cut OR filter your count variable
by 20 or any such value! Here, rows with n >3 are subset. Done.
df2 <- df1[df1$n>3,]
df2
# A tibble: 4 x 6
# Groups: X0 [1]
X1 X2 X3 X4 X0 n
<dbl> <dbl> <dbl> <dbl> <chr> <int>
1 -0.255 1.05 -0.812 -0.587 C 4
2 0.560 -0.790 -0.954 -0.0386 C 4
3 0.984 0.986 -0.490 0.452 C 4
4 0.939 0.549 -0.418 -0.405 C 4
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句