I have been trying to do this but not getting anywhere. Any help will be very much appreciated.
df1 <- data.frame(chrom = "chr1", start=c(10,20,30), end = c(100,200,300), stringsAsFactors=FALSE)
df2 <- data.frame(chrom = c("chr1", "chr2", "chr3"),start=c(15,500,150), end = c(75,1000,300), stringsAsFactors=FALSE)
I want to get all rows of df2 where df1$chrom == df2$chrom. Or better yet: I want to generate the output in a new vector and display the rows of df1 followed by df2 or vice versa where df1$chrom == df2$chrom.
I have tried this using a for loop as follows:
for(i in 1:nrow(df2)){
x[i] <- df2[which(df1$chrom == df2$chrom[i])]
}
Not working!