Average column value within a table with R

Asked

Viewed 43 times

1

I need to generate the average in a column of value of a table in R, but I’m not getting it. I’ve done so creating a vector:

x <- c(15,12,8,9,5,6,1,2)
mean(x)

It works, but when I point to the table I generated inside the R I cannot:

x <- (receitas_vs_depesas_contratadas_tidy %>%
  select.list(VR_TOTAL_RECEITA))%>%

mean(x)

erro:Error in (receitas_vs_depesas_contratadas_tidy %>% select.list(VR_TOTAL_RECEITA)) %>%  : 
  could not find function "%>%"
  • 3

    Load the package dplyr or tidyverse to gain access to the command %>%. After that, the command (receitas_vs_depesas_contratadas_tidy %>% select.list(VR_TOTAL_RECEITA))%>% will continue to give error. Please, take a look at this link and here’s how to ask a playable question in R. Edit your question with the necessary details so we can help you in the best way possible.

  • imported the package and changed the code:

  • media<-c(receitas_vs_depesas_contratadas_tidy %>% select(VR_TOTAL_DESPESA)%>% mutate(media=Mean(VR_TOTAL_DESPESA)))

  • but has not yet resolved

  • That’s why I posted the link above. If we don’t have a piece of your data set, we’re gonna sit here groping in the dark and not come to any conclusions. Edit your question to make it playable. It will be more laborious at first, but everyone here will save you time, including yourself.

  • If you just want the column average with the package dplyr nor is it necessary to select. See this example with the dataset iris that comes with the base R. iris %>% summarise(media = mean(Sepal.Length)). Note that it is summarise, is not mutate.

Show 1 more comment

1 answer

1

Using the summarise_all package dplyr.

library(dplyr)
set.seed(1)
x <- c(15,12,8,9,5,6,1,2)

df <- data.frame(x,
                 y = rnorm(length(x)),
                 w = rnorm(length(x)))

df %>% 
  dplyr::summarise_all(mean)
> df %>% 
+   dplyr::summarise_all(mean)
     x         y          w
1 7.25 0.1314544 0.05200928

Note that the average response is equal to the average of each column, when used the $.

mean(df$x)
#
> mean(df$x)
[1] 7.25
mean(df$y)
#
> mean(df$y)
[1] 0.1314544
mean(df$w)
#
> mean(df$w)
[1] 0.05200928

Browser other questions tagged

You are not signed in. Login or sign up in order to post.