R Subset "[..]" wrong values

Viewed 61

I am pretty selftought in R so I dont have that much expertise. But when I recently checked some "tripple"-subsetted data, I recognized some errors which I cant explain, even after several expereminental attempts.

> data <- read.csv("MEC7 table_R.csv", header = TRUE, sep = ";")
> show(data)
   Accession Nitrogen.supply Replicate chlorophylle.A chlorophylle.B carotenoids  phenols flavanoids carbohydrates   proteins
1      HM022             Fix         2       35.71822       13.61773    5.328781 349.0419  116.20386      108.6696   4.602691
2      HM022             Fix         4       72.79944       26.31981   12.419295 324.1432         NA      113.3090  10.127329
3      HM022             Fed         3       40.84556       14.78139    7.844996 424.2241  118.89316      149.3712   8.624055
4      HM022             Fed         4       51.43829       18.34177   10.417405 428.2580         NA      120.8249  71.665152
5      HM022             Fix         3       55.42231       18.73236    5.644060 210.9737  104.85056      391.5803   6.043350
6      HM022             Fed         2       55.77691       18.66710    7.606938 359.6686  117.05009      271.1971   7.368690
7      HM306             Fed         3       51.23863       20.18586    8.830432 444.5283  158.76851      324.4583   2.757425
8      HM306             Fix         4       90.45100       31.74905   14.505800 388.7416  113.75900      250.3901  16.132638
9      HM022             Fix         1       66.61150       22.31275   11.134300 328.1411  152.92800      387.9007   7.821263
10     HM022             Fed         1       67.63950       21.24895   12.076750 483.3238  130.51800      273.0234   6.382024
11     HM306             Fed         2       65.51469       22.45891   13.603800       NA         NA            NA         NA
12     HM306             Fix         2      117.65653       37.67211   20.725213       NA         NA            NA         NA
13     HM306             Fix         3      100.54241       34.42628   20.371192       NA         NA     1273.3765 244.340559
14     HM306             Fed         1       58.26609       22.47596   10.582551 317.4561   99.01719      319.1538   5.822906
15     HM306             Fed         1       66.12246       22.63671   13.222049 399.5537   96.86456      437.4982   4.082517
16     HM306             Fed         4       84.31291       29.05635   17.092142 411.3784   75.22140      387.2773   5.593258
> Chla <- data$chlorophylle.A
> Chla022 <- Chla[data$Accession=="HM022"]
> Chla022Fix <- Chla022[data$Nitrogen.supply=="Fix"]
> Chla022Fix
[1] 35.71822 72.79944 55.42231 67.63950       NA       NA       NA   

As u can see the value of line 9 ("Fix") was confused with that from line 10 ("Fed") and similar errors appear in other columns as well (chlb, carotenoids etc.). Also the NA values are not clear to me. Do I miss something? This is very disturbing for me. Thanks for help in advance :)

1 Answers

Is this what you are looking for?

library(dplyr)

df %>%
  filter(Accession=="HM022" & Nitrogen.supply=="Fix") %>%
  pull(chlorophylle.A)

[1] 35.71822 72.79944 55.42231 66.61150

And in base R you can do following solution. Here [[() is an equivalent to function(x) x[["chlorophylle.A"]] , in case you would like to extract chlorophylle.A column of your data frame.

`[[`(subset(df, Accession=="HM022" & Nitrogen.supply=="Fix"), "chlorophylle.A")

[1] 35.71822 72.79944 55.42231 66.61150

Or in case you wanted to stick by your own code. First keep in mind that we subset our data set by rows where variables Accession and Nitrogen.supply meet our requirements. After that we extract our desired column which is chlorophylle.A:

df <- df[df$Accession == "HM022" & df$Nitrogen.supply == "Fix", ] 
df <- df$chlorophylle.A

[1] 35.71822 72.79944 55.42231 66.61150
Related