r - Loop a if statment through rows -
i new r asking basic question.
i have 2 data frames
data frame 1 contains matching pairs:
factor1 factor2 d b e c f
data frame 2 contains level of different factors in different samples:
sample1 sample2 10 0 b 10 0 c 0 0 d 0 10 e 0 10 f 0 0
i trying loop through first data frame. each row in data frame 1, if level of factor1 larger 5 in sample 1 , level of factor 2 larger 5 in sample 2, add true in third column data frame 1. otherwise add false. hope question clear enough. thanks
factor1 factor2 if_match d true b e true c f false
assuming number of columns same in 2 datasets, use match
index of rows based on comparing rownames of 'df2' of each column of 'df1', corresponding column value of second dataset, check if greater or equal 5 , reduce
single logical vector
df2$if_match <- reduce(`|`, map(function(x, y) y[match(row.names(df2), x, nomatch = 0)] >=5 , df1, df2)) df2$if_match #[1] true true false
Comments
Post a Comment