Home > Net >  R Loop to calculate mean for each of two variables
R Loop to calculate mean for each of two variables

Time:05-26

I have a dataset (df) where I need to calculate the mean count for each city and house.

I wanted to/ I need to use a loop for that. However, I am not proficient.

I have something like:

for (i in 1:df$City) { 
for (j in 1:df$House) {
    mean_count[i] <- mean(df$Count)
  }
}

But this is not working. I am very new to loops so I don't know what is wrong. Error message is

"Error in 1:df$City: NA/NaN argument In addition: Warning messages: 1: In 1:df$City: numerical expression has 10383 elements: only the first used 2: In 1:df$City: NAs introduced by coercion"

The sample data:

City   House Count
Poz     1    7
Wre     4    8
KRK     4    5
Poz     2    13
KRK     3    7
Poz     4    45
Wre     8    15
Lub     8    9

CodePudding user response:

Normally, you would not need a loop for this. If your data is dt, then you can estimate the mean for each City/House combination doing this:

library(dplyr)
dt %>%
  group_by(City,House) %>%
  summarize(mean_count = mean(Count,na.rm=T))

CodePudding user response:

You can use base R as follows :

aggregate(df$Count , by = list( city = df$City , house = df$House) , mean)
  • Related