Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Subsetting data by a unique ID

$
0
0

I try to subset data based on an ID (can be any number) and one condition. An example could be;

I have several cars with a different number of cilinders and a different number of carburetors. I want a subset for every unique value of cilinders. And in the subsets of cilinders i want again a subset for every unique value of carburetors.

What i tried so far is to subset the mtcars data by the unique number of cilinders. This worked fine and it gave me 3 subsets. I used this to do so;

# Loading
data(mtcars)

mtcars_split <- split(mtcars, mtcars$cyl)
new_names <- c("subset1", "subset2", "subset3", "subset4")
for (i in 1:length(mtcars_split)) {
  assign(new_names[i], mtcars_split[[i]])
}        

Subset 4 is not used since there are only three different number of cilinders in the mtcars dataset.

But now I want to do the same for subset 1, subset 2 and subset 3 with the number of carburetors.

I then tried

#For cylinder 4, carb 1 and 2 
mtcars_split2 <- split(subset1, subset1$carb)
new_names <- c("subset1carb1", "subset1carb2")
for (i in 1:length(mtcars_split2)) {
  assign(new_names[i], mtcars_split2[[i]])
}

#for cyclinder 6, carb 1,2 and 3
mtcars_split3 <- split(subset2, subset2$carb)
new_names <- c("subset2carb1", "subset2carb2", "subset2carb3")
for (i in 1:length(mtcars_split3)) {
  assign(new_names[i], mtcars_split3[[i]])
}

#for cyclinder 8, carb 1,2,3 and 4
mtcars_split4 <- split(subset3, subset3$carb)
new_names <- c("subset3carb1", "subset3carb2", "subset3carb3", "subset3carb4")
for (i in 1:length(mtcars_split4)) {
  assign(new_names[i], mtcars_split4[[i]])
}

#etc

However there must an easier way to do this? In large datasets this manual solution takes way too much time. You get a ton of different combinations that you all have to define at some point.

It would be great if R could somehow automatically generate and name the unique subsets based on these 2 conditions. cilinders and carburetors.


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>