I have a dataframe which looks like this:
id age1 sex1 age2 sex2 age3 sex3 age4 sex4
1 5 20 <NA> NA <NA> NA <NA> 27 Female
2 25 NA <NA> NA <NA> NA <NA> 35 Female
3 65 NA <NA> NA <NA> NA <NA> NA <NA>
this is the code for the data:
temp <- structure(list(id = c(5L, 25L, 65L, 25L, 65L, 5L, 5L, 85L, 285L,
541L), age1 = c(20L, NA, NA, NA, NA, NA, NA, NA, NA, NA), sex1 = structure(c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Label = c("missing",
"inapplicable", "refusal", "don't know", "inconsistent", "Male",
"Female"), class = "factor"), age2 = c(NA, NA, NA, NA, 31L,
NA, NA, NA, NA, NA), sex2 = structure(c(NA, NA, NA, NA, 7L,
NA, NA, NA, NA, NA), .Label = c("missing", "inapplicable", "refusal",
"don't know", "inconsistent", "Male", "Female"), class = "factor"),
age3 = c(NA, NA, NA, NA, 32L, NA, NA, NA, 25L, 23L), sex3 = structure(c(NA,
NA, NA, NA, 7L, NA, NA, NA, 6L, 7L), .Label = c("missing",
"inapplicable", "refusal", "don't know", "inconsistent",
"Male", "Female"), class = "factor"), age4 = c(27L, 35L,
NA, NA, 33L, NA, 24L, NA, 26L, NA), sex4 = structure(c(7L,
7L, NA, NA, 7L, NA, 7L, NA, 6L, NA), .Label = c("missing",
"inapplicable", "refusal", "don't know", "inconsistent",
"Male", "Female"), class = "factor")), row.names = c(NA,
10L), class = "data.frame")
I would like to know how to make multiple subsets based the data based on the columns.
I know I could do this by using the codes:
Subset1<- temp[,1:3]
Subset2<-temp[,c(1,4:5)]
Subset3<- temp[,c(1,6:7)]
But there must be a more concise way to do this. I've tried a for loop but I'm new to R and don't know how to this including keeping the names of the new subsets consistent.
We can use
split.defaultto split data based on number in the column names and append the first column in each list.This returns a list of dataframes, if you want data in separate dataframes, we can do :