smicaela
smicaela

Reputation: 109

Using split() in a for loop

I would like to create a loop that gets the summary split on factor levels for each variable. For example, if I wanted to get summary split by factor levels within "grouping" variable, I would use:

df %>%
select(grouping, length, weight) %>%
split(.$grouping) %>%
map(summary)

However, I am not sure how to put this into a loop, such that I get a summary based on the factor levels of each of the variables of interest within my dataframe.

For example, I can get the summary() for variables in columns 3 and 4 of dataframe using:

# Dummy data
length = sample(30:60, 10, replace = FALSE)  
weight = sample(50:70, 10, replace = FALSE)
grouping = c("A", "A", "B", "A", "B", "A", "B", "B", "B", "A")
colour = c("Blue", "Green", "Green", "Green", "Blue", "Blue", "Blue", "Green", "Blue", "Green")
type = c("Case", "Control", "Case", "Case", "Case", "Control", "Control", "Case", "Control", "Case")
df = data.frame(length, weight, grouping, colour, type)

# Variables to loop
colNames <- names(df)[c(3:4)]

# Summary
for(i in colNames){
  # Summary
  summary <- df %>%
    select(length, weight, .$colNames[i]) %>%
    summary()
  
  print(summary)
}

But I can't do it when split by factor levels for each variable:

# Variables to loop 
colNames = names(df)[c(3,4)]

# Summary 
for(i in colNames){
df %>%
select(length, weight, .$colNames[i]) %>%
split(.$colNames[i]) %>%
summary()
}

I figure split(.colNames) is the problem, but I am not sure how to fix it. Thank you for any help!

Upvotes: 2

Views: 196

Answers (1)

stefan
stefan

Reputation: 125488

There are two issues with your code:

  1. i already is the name of your column. Hence .$colNames[i] is NULL. This issue already arises in select.
  2. If you want to access a column in a dataframe with a variable containing the column name then $ will not work. Instead use [[.
# Dummy data
set.seed(42)
length <- sample(30:60, 10, replace = FALSE)
weight <- sample(50:70, 10, replace = FALSE)
grouping <- c("A", "A", "B", "A", "B", "A", "B", "B", "B", "A")
colour <- c("Blue", "Green", "Green", "Green", "Blue", "Blue", "Blue", "Green", "Blue", "Green")
type <- c("Case", "Control", "Case", "Case", "Case", "Control", "Control", "Case", "Control", "Case")
df <- data.frame(length, weight, grouping, colour, type)

# Variables to loop
colNames <- names(df)[c(3:4)]

library(dplyr)
library(purrr)

# Summary
for (i in colNames) {
  df %>%
    select(length, weight, all_of(i)) %>%
    split(.[[i]]) %>% 
    map(summary) %>% 
    map(print)
}

#>      length         weight       grouping        
#>  Min.   :33.0   Min.   :52.0   Length:5          
#>  1st Qu.:34.0   1st Qu.:53.0   Class :character  
#>  Median :36.0   Median :54.0   Mode  :character  
#>  Mean   :40.6   Mean   :59.2                     
#>  3rd Qu.:46.0   3rd Qu.:67.0                     
#>  Max.   :54.0   Max.   :70.0                     
#>      length       weight       grouping        
#>  Min.   :30   Min.   :58.0   Length:5          
#>  1st Qu.:39   1st Qu.:60.0   Class :character  
#>  Median :44   Median :63.0   Mode  :character  
#>  Mean   :44   Mean   :62.2                     
#>  3rd Qu.:47   3rd Qu.:64.0                     
#>  Max.   :60   Max.   :66.0                     
#> NULL
#>      length         weight        colour         
#>  Min.   :33.0   Min.   :52.0   Length:5          
#>  1st Qu.:39.0   1st Qu.:53.0   Class :character  
#>  Median :44.0   Median :58.0   Mode  :character  
#>  Mean   :41.8   Mean   :57.4                     
#>  3rd Qu.:46.0   3rd Qu.:60.0                     
#>  Max.   :47.0   Max.   :64.0                     
#>      length         weight      colour         
#>  Min.   :30.0   Min.   :54   Length:5          
#>  1st Qu.:34.0   1st Qu.:63   Class :character  
#>  Median :36.0   Median :66   Mode  :character  
#>  Mean   :42.8   Mean   :64                     
#>  3rd Qu.:54.0   3rd Qu.:67                     
#>  Max.   :60.0   Max.   :70

Upvotes: 2

Related Questions