I have a dataframe from which I want to extract some rows that meet a condition. I am already able to succesfully get the index of those rows, but when I try to append those rows to the empty dataframe I've created I have all kinds of errors (or even funnier, no errors but an empty dataframe).
datosA <- read_excel(archivo, col_names = TRUE, sheet = "Sheet1")
datosB <- read_excel(archivo, col_names = TRUE, sheet = "Sheet1")
AvsB <- data.frame()
for (x in datosA$'m/z') {
if (!(x %in% datosB$'m/z')) {
index <- match(x, datosA$'m/z')
rbind(AvsB, datosA[index,])
}
}
Unfortunately this does nothing.
head(datosA)
# A tibble: 6 x 16
Row Index `Peak Name` `m/z` `Ret. Time` `HC 1` `HC 2` `QC 1` `FM 1` `HC 3` `QC 2` `FM 2` `QC 3` `FM 3` `QC 4` `FM 4`
<dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 1 288.3/7.8 (80) 288. 7.82 82639 133150. 115280 32179 45842 116433 37358 138583 32425 217890 37055
2 2 2 288.3/8.0 (82) 288. 7.96 85895. 140309 119273 15051 63766 132999 12131 146485 13557 245226 14266
3 3 3 301.1/1.3 (92) 301. 1.28 141133. 161141. 180196 22494 168913 185736 26525 172954 29359 156029 25718
4 4 4 313.3/15.7 (99) 313. 15.7 92327. 116870 90781 816 84477. 126151 801 126316 971 186423 990
5 5 5 316.3/11.9 (104) 316. 11.9 12232. 8196. 10279 660 15646 15529 679 20439 657 17514 576
6 6 6 316.3/8.5 (105) 316. 8.47 98679 111867. 105794 62869 74714. 111100 77540 153424 80128 159846 76565
You need to store the result of rbind back into AvsB in each loop:
datosA <- read_excel(archivo, col_names = TRUE, sheet = "Sheet1")
datosB <- read_excel(archivo, col_names = TRUE, sheet = "Sheet1")
AvsB <- data.frame()
for (x in datosA$'m/z') {
if (!(x %in% datosB$'m/z')) {
index <- match(x, datosA$'m/z')
AvsB <- rbind(AvsB, datosA[index,])
}
}