-1

我对 R 真的很陌生,遇到了以下问题。我的数据框看起来像这样。

    >   Entity  Substance conc..mM. variable value
    1     STD5    Sulfate     5.000     Area 70761
    2     STD5    Formate     5.000     Area    NA
    3     STD5    Acetate     5.000     Area 70777
    4     STD5    Lactate     5.000     Area 74329
    5     STD5 Propionate     5.000     Area 84198
    6     STD5   Butyrate     5.000     Area 91598
    7     STD5  Valeriate        NA     Area 40609
    8     STD5   Caproate        NA     Area 40763
    9   STD2_5    Sulfate     2.500     Area 34350
    10  STD2_5    Formate     2.500     Area    NA
    11  STD2_5    Acetate     2.500     Area 34863
    12  STD2_5    Lactate     2.500     Area 36262
    13  STD2_5 Propionate     2.500     Area 41849
    14  STD2_5   Butyrate     2.500     Area 44574
    15  STD2_5  Valeriate     2.500     Area 39664
    16  STD2_5   Caproate     2.500     Area 39867

现在我想添加一个列,其中给定实体的每一行都包含来自相应实体的 caproate 值。有没有聪明的方法来实现这一目标?

预期产出

    >   Entity  Substance conc..mM. variable value area caproate
    1     STD5    Sulfate     5.000     Area 70761   40763
    2     STD5    Formate     5.000     Area    NA   40763 
    3     STD5    Acetate     5.000     Area 70777   40763
    4     STD5    Lactate     5.000     Area 74329   40763
    5     STD5 Propionate     5.000     Area 84198   40763
    6     STD5   Butyrate     5.000     Area 91598   40763
    7     STD5  Valeriate        NA     Area 40609   40763
    8     STD5   Caproate        NA     Area 40763   40763
    9   STD2_5    Sulfate     2.500     Area 34350   39867
    10  STD2_5    Formate     2.500     Area    NA   39867
    11  STD2_5    Acetate     2.500     Area 34863   39867
    12  STD2_5    Lactate     2.500     Area 36262   39867
    13  STD2_5 Propionate     2.500     Area 41849   39867
    14  STD2_5   Butyrate     2.500     Area 44574   39867
    15  STD2_5  Valeriate     2.500     Area 39664   39867
    16  STD2_5   Caproate     2.500     Area 39867   39867
4

2 回答 2

2

几个选项:

使用plyr

library(plyr)
ddply(dat,.(Entity), transform,newv=value[Substance =="Caproate"])

或者,如果您有大量数据,请使用data.table

library(data.table)
DT <- as.data.table(dat)
DT[,newv:=value[Substance =="Caproate"],by=Entity]
于 2013-11-03T09:14:53.317 回答
0

使用 R 基函数ave

> dat$area_caproate <- with(dat, ave(value, Entity, FUN=function(x) na.omit(x[Substance=="Caproate"])))
> dat
   Entity  Substance conc..mM. variable value area_caproate
1    STD5    Sulfate       5.0     Area 70761         40763
2    STD5    Formate       5.0     Area    NA         40763
3    STD5    Acetate       5.0     Area 70777         40763
4    STD5    Lactate       5.0     Area 74329         40763
5    STD5 Propionate       5.0     Area 84198         40763
6    STD5   Butyrate       5.0     Area 91598         40763
7    STD5  Valeriate        NA     Area 40609         40763
8    STD5   Caproate        NA     Area 40763         40763
9  STD2_5    Sulfate       2.5     Area 34350         39867
10 STD2_5    Formate       2.5     Area    NA         39867
11 STD2_5    Acetate       2.5     Area 34863         39867
12 STD2_5    Lactate       2.5     Area 36262         39867
13 STD2_5 Propionate       2.5     Area 41849         39867
14 STD2_5   Butyrate       2.5     Area 44574         39867
15 STD2_5  Valeriate       2.5     Area 39664         39867
16 STD2_5   Caproate       2.5     Area 39867         39867
于 2013-11-03T11:02:38.917 回答