I would like to do an if else statement with multiple conditions. I have two data frames, first one looks like this:
prefix <- "sample"
suffix <- seq(1:100)
id <- paste(prefix, suffix, sep="")
indv_df <- data.frame(id, count = matrix(ncol=1, nrow=100))
And the first 15 rows of indv_df looks like this:
id count
1 sample1 NA
2 sample2 NA
3 sample3 NA
4 sample4 NA
5 sample5 NA
6 sample6 NA
7 sample7 NA
8 sample8 NA
9 sample9 NA
10 sample10 NA
11 sample11 NA
12 sample12 NA
13 sample13 NA
14 sample14 NA
15 sample15 NA
The second table called row1 that I have looks like this:
Hom <- paste("sample2", "sample3", "sample4", sep=",")
Het <- paste("sample5", "sample6", "sample7", sep=",")
Missing <- paste("sample10", "sample11", sep=",")
row1 <- data.frame(Hom, Het, Missing)
looks like this:
Hom Het Missing
1 sample2,sample3,sample4 sample5,sample6,sample7 sample10,sample11
I am trying to do if else statement that if the first row's id does not match any of the second table's content, write "0" in the first table's first row, second column. This is what I tried but didn't work, which I am not too surprised since this is my first if else statement. I know it should be straight forward but I tried a few different methods none worked
> if(grep(indv_df$id[1], row1$Hom)){
+ apply(indv_df[1,2]=="2")
+ } else if(grep(indv_df$id[1], row1$Het)){
+ apply(indv_df[1,2]=="1")
+ } else if(grep(indv_df$id[1], row1$Missing)){
+ apply(indv_df[1,2]=="missing")
+ } else (apply(indv_df[1,2]=="0"))
this is the error message I got:
Error in if (grep(indv_df$id[1], row1$Hom)) { :
argument is of length zero
The real dataset has 4 million rows in the second data.frame, so I am just testing the first step..... once I get through this I will try to do that in a loop for all rows. :D Thank you for all the help in advance.
Aucun commentaire:
Enregistrer un commentaire