简体   繁体   中英

Match/subset one dataframe based on conditional values in another dataframe in R

I have two data frames:

df1=data.frame(A=c(1,2,4,8), B=c(4,3,2,9), C=c(10,11,1,2), D=c(12,40,3,4))
df2=data.frame(A=c(0.5,2.0,0.1,0.3), B=c(1.5,0.5,0.2,0.1), C=c(3.0,1.25,0.5,0.2), D=c(0.7,0.8,0.2,2.0))

I want to keep values in df1 that are <= 0.8 in df1 for all columns and NA s in the ones that are > 0.8

I tried to find and replace values > 0.8 in df2:

df2[df2 >= 0.8] <- NA

Then I tried to replace all matching values in df1 with the NA in df2 but something like the script below wants columns not dataframes:

df1[match(df1, df2==NA)] 

I want the final dataframe to look like this:

df3=data.frame(A=c(1,NA,4,8), B=c(NA,3,2,9), C=c(NA,NA,1,2), D=c(12,40,3,NA))

TIA

Use mapply like this:

as.data.frame(mapply(function(x, y) ifelse(y <= 0.8, x, NA), df1, df2))

or

replace(df1, df2 > 0.8, NA)

We can directly assign NA based on the logical matrix

 NA^(df2 > 0.8) * df1

or

`is.na<-`(df1, df2 > 0.8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM