0

hello I have a df called df and I have subsetted it in another df called df1. Now I'd like to remove df1 rows from df to obtain a df2 = df - df1. How I can do it on R?

df <- read.csv("dataframe.csv")
 
df1 <- df[(df$time <= 0.345),]
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
Nancy
  • 101
  • 1
  • 8

3 Answers3

1

Try:

df2 <- df[(df$time > 0.345), ]

or

df2 <- df[-which(df$time <= 0.345), ]
Leonardo
  • 2,439
  • 33
  • 17
  • 31
1

If for any reason you strictly have to keep the structure described, this is a possible approach:

df = data.frame(Sample.Name = c(12,13,14,12,13), 
                Target=c("A","B","C","A","A"), 
                Task=c("Sample","Standard","Sample","Standard","Sample"), 
                Value=c(36,34,34,35,36), 
                Mean=c(35,32,36,37,35))

df1 = df[(df$Value <= 34),]

df2 = df[do.call(paste0, df) %in% do.call(paste0, df1),]

df2

The result is this one:

  Sample.Name Target     Task Value Mean
2          13      B Standard    34   32
3          14      C   Sample    34   36
rodrigocfaria
  • 440
  • 4
  • 11
1

This should work without even knowing the logic of first subset

library (dplyr) 
df2 <- setdiff(df, df1) 

OR

df2 <- anti_join(df, df1) 
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45