Skip to content
Prev 307856 / 398503 Next

Filtering few column from one data frame based on another data frame

Hi Rui,

By running your code on an example dataset:
set.seed(1)
dat1<-data.frame(Sno=sample(1:50,10,replace=TRUE),Data_1=rnorm(10,15),Data_2=rnorm(10,25),Data_3=runif(10,0.5))

dat2<-read.table(text="
Data_2
Data_3
",sep="",header=FALSE,stringsAsFactors=FALSE)


keep <- which(!names(dat2) %in% names(dat1))
dat1[, keep]
#[1] 14 19 29 46 11 45 48 34 32? 4
I guess, the OP needs both "Sno" and "Data_1" i the resultant dataframe.

#My code:
res<-dat1[!colnames(dat1)%in%dat2[,1]]
head(res) 
# Sno?? Data_1
#1? 14 14.17953
#2? 19 15.48743
#3? 29 15.73832
#4? 46 15.57578
#5? 11 14.69461
#6? 45 16.51178
A.K.



----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: siddu479 <onlyfordigitalstuff at gmail.com>
Cc: r-help at r-project.org
Sent: Saturday, October 13, 2012 12:10 PM
Subject: Re: [R] Filtering few column from one data frame based on another data frame

Hello,

Try the following.

keep <- which(!names(data2) %in% names(data1))
data1[, keep]

Hope this helps,

Rui Barradas
Em 13-10-2012 15:48, siddu479 escreveu:
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.