0

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
  • 3
    Although 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. Commented Feb 24, 2015 at 13:52
  • 1
    I'm voting to close this question as off-topic because it is about how to use R w/o a reproducible example. Commented Feb 24, 2015 at 14:11

1 Answer 1

1

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))]
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, You can mark the question as answered if you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.