[英]Combining multiple rows into one row with multiple columns of data R
I have a dataset where the same effect was measured in different ways and I want to compare those measurements.我有一个数据集,其中以不同的方式测量了相同的效果,我想比较这些测量结果。 My dataset looks like this:
我的数据集如下所示:
Study MType ID Insect Mean Sd N
Alla Fecundity 1 Aphid .62 .7628 11
Alla RGR 1 Aphid -32.8 7.76 11
Ando Survival 2 Bee 2.34 .67 8
Ando RGR 2 Bee 4.56 .34 10
Ando Fecundity 2 Bee 5.32 4.3 20
I want to combine the rows by ID number so that the MType, Mean, Sd and N for each row are preserved (although the column names need to change so the columns are distinguishable).我想按 ID 号合并行,以便保留每行的 MType、Mean、Sd 和 N(尽管需要更改列名以便区分列)。
Hopefully, in the end it would look like:希望最终它看起来像:
Study ID Insect Fecundity.mean Fecundity.Sd Fecundity.N RGR.mean RGR.Sd...etc
Some difficulties:一些困难:
I have messed around with reshape and with tidyr and I haven't been able to figure out how to do this with either of them.我搞砸了 reshape 和 tidyr ,我一直无法弄清楚如何用它们中的任何一个来做到这一点。 Please help!
请帮忙!
You can use reshape
via base R. You want to transform your data from long to wide format according to this post: How to reshape data from long to wide format?您可以通过 base R 使用
reshape
。您想根据这篇文章将数据从长格式转换为宽格式: How to reshape data from long to wide format? . .
If your data is in a data.frame
d:如果您的数据在
data.frame
d 中:
reshape(d, idvar=c("ID", "Study", "Insect"), timevar = "MType", direction="wide")
Results:结果:
Study ID Insect Mean.Fecundity Sd.Fecundity N.Fecundity Mean.RGR Sd.RGR N.RGR Mean.Survival Sd.Survival N.Survival
1 Alla 1 Aphid 0.62 0.7628 11 -32.80 7.76 11 NA NA NA
3 Ando 2 Bee 5.32 4.3000 20 4.56 0.34 10 2.34 0.67 8
Doing this with tidyr
is not obvious, because you have to first gather()
and then spread()
.使用
tidyr
执行此操作并不明显,因为您必须先gather()
然后再spread()
。
library(tidyverse)
example <- tribble(
~Study, ~MType, ~ID, ~Insect, ~Mean, ~Sd, ~N,
"Alla", "Fecundity", 1, "Aphid", .62, .7628, 11,
"Alla", "RGR", 1, "Aphid", -32.8, 7.76, 11,
"Ando", "Survival", 2, "Bee", 2.34, .67, 8,
"Ando", "RGR", 2, "Bee", 4.56, .34, 10,
"Ando", "Fecundity", 2, "Bee", 5.32, 4.3, 20)
gather(example, key = "Statistic", value = "value", Mean, Sd, N) %>%
unite(col="MType.Statistic", MType, Statistic, sep = ".") %>%
spread(key = MType.Statistic, value=value)
#> # A tibble: 2 x 12
#> Study ID Insect Fecundity.Mean Fecundity.N Fecundity.Sd RGR.Mean
#> * <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Alla 1.00 Aphid 0.620 11.0 0.763 -32.8
#> 2 Ando 2.00 Bee 5.32 20.0 4.30 4.56
#> # ... with 5 more variables: RGR.N <dbl>, RGR.Sd <dbl>,
#> # Survival.Mean <dbl>, Survival.N <dbl>, Survival.Sd <dbl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.