R Loop to calculate mean for each of two variables

TerrorOrange

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
langtang

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))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

declaring two variables to calculate a function in a for loop in R

From Dev

R the mean of each iteration of the for loop

From Dev

R Two Variables in Loop

From Dev

How do I calculate the mean of two different variables taking into account the values of latitude and longitude in R?

From Dev

for loop in R to calculate mean from list

From Dev

Running a for loop in r over columns to calculate mean

From Dev

How to calculate mean of two timestamp columns in R?

From Dev

Calculate mean of dataset in R that satisfies two conditions

From Dev

python for loop to calculate mean value for each array segment

From Dev

R: Get the mean for each column with the loop for

From Dev

How to calculate mean value of two list for each row

From Dev

how to calculate mean values of the past n years grouped by two variables

From Dev

R - Mean of two variables containing NA

From Dev

How to calculate the proportion of two categorical variables in R

From Dev

Calculate the mean of values in a loop

From Dev

Calculate mean of each row in a large list of dataframes in R

From Dev

R: combn pair of variables calculate the mean between them

From Dev

Calculate the mean of a dataframe column for every five variables in R

From Dev

R: How to loop over a name-based selection of variables from a dataframe and for each create a new variable containing the column mean of the first?

From Dev

Calculate column sums for each combination of two grouping variables

From Dev

Loop through multiple folder and Calculate mean of raster of Same folder R

From Dev

Calculate the weighted mean for a variable, which has two types with R

From Dev

Calculate Mean for each CSV row

From Dev

How to loop through two variables at once in r

From Dev

Calculate mean of several xarray variables

From Dev

R - Calculate mean of rasterbrick

From Dev

Calculate percentage with two variables

From Dev

Use loop function in r to calculate constant value for each day?

From Dev

Is it possible to declare two variables For-Each Loop in C++?

Related Related

  1. 1

    declaring two variables to calculate a function in a for loop in R

  2. 2

    R the mean of each iteration of the for loop

  3. 3

    R Two Variables in Loop

  4. 4

    How do I calculate the mean of two different variables taking into account the values of latitude and longitude in R?

  5. 5

    for loop in R to calculate mean from list

  6. 6

    Running a for loop in r over columns to calculate mean

  7. 7

    How to calculate mean of two timestamp columns in R?

  8. 8

    Calculate mean of dataset in R that satisfies two conditions

  9. 9

    python for loop to calculate mean value for each array segment

  10. 10

    R: Get the mean for each column with the loop for

  11. 11

    How to calculate mean value of two list for each row

  12. 12

    how to calculate mean values of the past n years grouped by two variables

  13. 13

    R - Mean of two variables containing NA

  14. 14

    How to calculate the proportion of two categorical variables in R

  15. 15

    Calculate the mean of values in a loop

  16. 16

    Calculate mean of each row in a large list of dataframes in R

  17. 17

    R: combn pair of variables calculate the mean between them

  18. 18

    Calculate the mean of a dataframe column for every five variables in R

  19. 19

    R: How to loop over a name-based selection of variables from a dataframe and for each create a new variable containing the column mean of the first?

  20. 20

    Calculate column sums for each combination of two grouping variables

  21. 21

    Loop through multiple folder and Calculate mean of raster of Same folder R

  22. 22

    Calculate the weighted mean for a variable, which has two types with R

  23. 23

    Calculate Mean for each CSV row

  24. 24

    How to loop through two variables at once in r

  25. 25

    Calculate mean of several xarray variables

  26. 26

    R - Calculate mean of rasterbrick

  27. 27

    Calculate percentage with two variables

  28. 28

    Use loop function in r to calculate constant value for each day?

  29. 29

    Is it possible to declare two variables For-Each Loop in C++?

HotTag

Archive