简体   繁体   中英

R How to subset a dataframe with a for loop through a column

I want to create a series of data frames that are subsets of a main dataframe based upon the values in a particular factored column.

Below is my data and code:

>max_value_found

   Group  Mileage    HiLo 
1  car        808    Hi     
2  train   162993    Lo 
3  car      19386    Hi     
4  walk       231    Lo 



>levels(max_value_found$group)

[1] "car"       "train"     "bike"      "walk"

>for(i in levels(max_value_found$group))
+{  x=paste(i,"_max_value",sep="")
+   x
+   x <-subset(max_value_found, max_value_found$group==i)
+}

>car_max_value

Error: object 'car_max_value' not found

I don't get an error but I don't get any new data frames either, as far as I can tell using ls().

What I'm trying to do is have R loop through the list of levels and paste the level name in the resulting dataframe name and use that same level to subset the main dataframe for the subset's dataframe contents.

I could have just manually written out the individual subset statements but would like to know how to do this for the future.

I have similar code working for looping through the same column to generate a series of histograms but this is stumping me.

Thanks for your help

As David pointed out in the comments:

max_value_found <- data.frame(Group = c("car", "train", "car", "walk"),
                              Mileage = c(808, 162993, 19386, 231), 
                              HiLo = c("Hi", "Lo", "Hi", "Lo")) 


> list1 <- split(max_value_found, max_value_found$Group)
> list1 
$car   
Group Mileage HiLo 
1   car     808   Hi 
3   car   19386   Hi

$train   
Group Mileage HiLo 
2 train  162993   Lo

$walk   
Group Mileage HiLo 
4  walk     231   Lo

Edit : to set these free into the global environment again use David's code:

> list2env(list1, .GlobalEnv)
<environment: R_GlobalEnv>
> ls()
[1] "car"             "list1"           "max_value_found" "train"          
[5] "walk"           
> str(car)
'data.frame':   2 obs. of  3 variables:
 $ Group  : Factor w/ 3 levels "car","train",..: 1 1
 $ Mileage: num  808 19386
 $ HiLo   : Factor w/ 2 levels "Hi","Lo": 1 1
> str(train)
'data.frame':   1 obs. of  3 variables:
 $ Group  : Factor w/ 3 levels "car","train",..: 2
 $ Mileage: num 162993
 $ HiLo   : Factor w/ 2 levels "Hi","Lo": 2
> car
  Group Mileage HiLo
1   car     808   Hi
3   car   19386   Hi
> train
  Group Mileage HiLo
2 train  162993   Lo

I don't think that this is a good move though. You are better off accessing the data frames from the list ie list1$car or list1[["car"]] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM