I have a data.frame with several columns, with some of them corresponding to numeric variables, and some - to categorical. How do I make a subset of this data.frame with only numeric or categorical variables?
2
-
3Although R is a tool regularly used in statistics, this question concerns coding rather than the use of statistics itself - as such it's better suited to StackOverflow.Silverfish– Silverfish2015-02-24 13:52:08 +00:00Commented Feb 24, 2015 at 13:52
-
1I'm voting to close this question as off-topic because it is about how to use R w/o a reproducible example.gung - Reinstate Monica– gung - Reinstate Monica2015-02-24 14:11:14 +00:00Commented Feb 24, 2015 at 14:11
Add a comment
|
1 Answer
It would be helpful if you could provide an example of the type of information you were looking to sort, however perhaps this is what you are looking for.
data <- data.frame(id=1:10,
type=sample(c("a,","b"), size=10,replace=TRUE),
age=sample(5:20,size=10, replace= TRUE),
colour=sample(c("red","blue","pink", "green"), size=10,replace=TRUE),
qty=sample(100:150, size= 10, replace= TRUE))
data[,"factor"==sapply(data,class)]
data[,"integer"==sapply(data,class)]
If you want to always include the ID column you can do the following
data[,c(TRUE,"factor"==sapply(data[,-1],class))]
1 Comment
Jonno Bourne
No problem, You can mark the question as answered if you want.