gpt4 book ai didi

r - dplyr:使用行而不是列进行变异

转载 作者:行者123 更新时间:2023-12-01 11:33:37 28 4
gpt4 key购买 nike

我的数据集是这样的

year Spp  CO2 plantN_mean  N15_mean plantN_sd    N15_sd plantN_se    N15_se
1 2004 A amb 17.136667 10.723333 1.2615202 0.7507552 0.7283391 0.4334487
2 2004 A elev 23.310000 13.043333 2.7160081 2.6595175 1.5680880 1.5354731
3 2004 AB amb 14.410000 10.156667 1.1363538 1.7773670 0.6560742 1.0261633
4 2004 AB elev 19.470000 14.786667 2.9173790 3.7358979 1.6843495 2.1569217
5 2004 AM amb 9.603333 13.510000 0.5515735 1.7176437 0.3184511 0.9916821
6 2004 AM elev 16.333333 9.743333 2.3622306 1.8825869 1.3638345 1.0869120

对于 plantN_meanN15_mean,我需要计算处理效果比,即 elev/amb。我可以使用 plyr 对其中一个变量执行此操作,如下所示:

effect <- ddply(data, .(year,Spp), function (x){
plantN_ratio <- x$plantN_mean[x$CO2 == "elev"]/x$plantN_mean[x$CO2 == "amb"]
data.frame(plantN_ratio)
})

plantN_meanN15_mean 的 dplyr 版本是什么?

我想答案可能是这样开始的:

effect <- summary %>% group_by(year,Spp) %>% mutate(
plantN_ratio=plantN_mean[CO2 == "elev"]/plantN_mean[CO2 == "amb"],
N15_ratio= N15_mean[CO2 == "elev"]/N15_mean[CO2 == "amb"])

最佳答案

tidyr 在这里很有用。

install_packages("tidyr")
library(tidyr)
library(dplyr) # for %>% from [magrittr][http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html]

可重现的数据:

df <- structure(list(year = c(2004L, 2004L, 2004L, 2004L, 2004L, 2004L
), Spp = c("A", "A", "AB", "AB", "AM", "AM"), CO2 = c("amb",
"elev", "amb", "elev", "amb", "elev"), plantN_mean = c(17.136667,
23.31, 14.41, 19.47, 9.603333, 16.333333), N15_mean = c(10.723333,
13.043333, 10.156667, 14.786667, 13.51, 9.743333)), .Names = c("year",
"Spp", "CO2", "plantN_mean", "N15_mean"), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"), .internal.selfref = <pointer: 0x0>)

df 看起来像:

  year Spp  CO2 plantN_mean  N15_mean
1 2004 A amb 17.136667 10.723333
2 2004 A elev 23.310000 13.043333
3 2004 AB amb 14.410000 10.156667
4 2004 AB elev 19.470000 14.786667
5 2004 AM amb 9.603333 13.510000
6 2004 AM elev 16.333333 9.743333

1。让我们收集所有的平均值变量:

gdf <- df %>% group_by(year,Spp) %>% gather(mean_id,mean_val,plantN_mean:N15_mean)

gdf 看起来像:

    year Spp  CO2     mean_id  mean_val
1 2004 A amb plantN_mean 17.136667
2 2004 A elev plantN_mean 23.310000
3 2004 AB amb plantN_mean 14.410000
4 2004 AB elev plantN_mean 19.470000
5 2004 AM amb plantN_mean 9.603333
6 2004 AM elev plantN_mean 16.333333
7 2004 A amb N15_mean 10.723333
8 2004 A elev N15_mean 13.043333
9 2004 AB amb N15_mean 10.156667
10 2004 AB elev N15_mean 14.786667
11 2004 AM amb N15_mean 13.510000
12 2004 AM elev N15_mean 9.743333

2。让我们根据 CO2 变量展开平均值:

sdf <- gdf %>% spread(CO2,mean_val)

sdf 看起来像:

  year Spp     mean_id       amb      elev
1 2004 A plantN_mean 17.136667 23.310000
2 2004 A N15_mean 10.723333 13.043333
3 2004 AB plantN_mean 14.410000 19.470000
4 2004 AB N15_mean 10.156667 14.786667
5 2004 AM plantN_mean 9.603333 16.333333
6 2004 AM N15_mean 13.510000 9.743333

3。现在计算 elev/amb 的比率:

sdf %>% mutate(elev_o_amb = elev / amb)

得到:

  year Spp     mean_id       amb      elev elev_o_amb
1 2004 A plantN_mean 17.136667 23.310000 1.3602412
2 2004 A N15_mean 10.723333 13.043333 1.2163506
3 2004 AB plantN_mean 14.410000 19.470000 1.3511450
4 2004 AB N15_mean 10.156667 14.786667 1.4558582
5 2004 AM plantN_mean 9.603333 16.333333 1.7007984
6 2004 AM N15_mean 13.510000 9.743333 0.7211942

关于r - dplyr:使用行而不是列进行变异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828124/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com