crazy_mouse
crazy_mouse

Reputation: 45

Multiple subsetting of a dataframe based on many conditions

At present I have a dataframe with 18 columns. I would like to subset the data in one of these columns, "present", according to combinations of data in six of the other columns within the data frame and then save this into a text file. The columns I would like to use to subset "present" are:

*answer (1:4) [answer takes values 1 to 4]
*p.num (1:18)
*session (1:2)
*count (1:8)
*type (1:3)

So there are 3456 possible subsetting combinations.

At present, I have been using the following and manually changing the values in each line and re-running the code.

input<-subset(input, answer==1)
input.s2g<-subset(input, p.num == 1)
input.s2g<-subset(input.s2g, session == "S2")
input.s2g<-subset(input.s2g, count==8)
input.s2g<-subset(input.s2g, type==1)

write.table(s2g, file = "1_1_S2_8_1", sep = "\t", col.names = F, row.names = F)

Yet this takes hours. There must be an easier way?

Upvotes: 2

Views: 139

Answers (1)

flodel
flodel

Reputation: 89057

I would try something along these lines:

splitted <- split(input, list(input$answer,
                              input$p.num,
                              input$session,
                              input$count,
                              input$type))

filenames <- gsub("\\.", "_", names(splitted))

mapply(write.table, splitted, file = filenames, sep = "\t",
                    col.names = FALSE, row.names = FALSE)

Upvotes: 3

Related Questions