Skip to content

Splitting up of a dataframe according to the type of variables

5 messages · Martin Spindler, jim holtman, Jessica Streicher +2 more

#
Dear R users,

I have a dataframe which consists of variables of type numeric and factor.
What is the easiest way to split up the dataframe to two dataframe which contain all variables of the type numeric resp. factors?

Thank you very much for your efforts in advance!

Best,

Martin
#
most likely the 'split' function, but exactly how would depend on the data which you did not provide.

Sent from my iPad
On Dec 17, 2012, at 5:02, "Martin Spindler" <Martin.Spindler at gmx.de> wrote:

            
#
f<-factor(c(1,1,2,3))
n<-c(1,1,2,3)
df<-data.frame(f,n)

sapply(df,is.factor)
f     n 
TRUE FALSE 
df[sapply(df,is.factor)]
f
1 1
2 1
3 2
4 3

 df[sapply(df,is.numeric)]
  n
1 1
2 1
3 2
4 3

something like that?
On 17.12.2012, at 11:02, Martin Spindler wrote:

            
#
Hello,

Try the following.

dat <- data.frame(X = rnorm(10), Y = factor(sample(letters, 10)), Z = 1:10)

num <- sapply(dat, is.numeric)
dat[num]

# or
dat[, num]


Hope this helps,

Rui Barradas
Em 17-12-2012 10:02, Martin Spindler escreveu:
#
Hi,
You could also use ?colwise() from library(plyr)
set.seed(50)
dat1<-data.frame(Col1=sample(1:20,10,replace=TRUE),Col2=sample(LETTERS[1:10],10,replace=TRUE),Col3=sample(LETTERS[11:20],10,replace=TRUE),Col4=sample(40:60,10,replace=TRUE))
?dat1[unlist(colwise(is.factor)(dat1))]
#?? Col2 Col3
#1???? D??? Q
#2???? C??? L
#3???? G??? R
#4???? A??? S
#5???? C??? N
#6???? G??? Q
#7???? I??? P
#8???? D??? M
#9???? A??? N
#10??? B??? N
?dat1[unlist(colwise(is.numeric)(dat1))]
A.K.




----- Original Message -----
From: Martin Spindler <Martin.Spindler at gmx.de>
To: R-help at r-project.org
Cc: 
Sent: Monday, December 17, 2012 5:02 AM
Subject: [R] Splitting up of a dataframe according to the type of variables

Dear R users,

I have a dataframe which consists of variables of type numeric and factor.
What is the easiest way to split up the dataframe to two dataframe which contain all variables of the type numeric resp. factors?

Thank you very much for your efforts in advance!

Best,

Martin

______________________________________________
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.