Create new columns containing average of other columns

Hello I'm new with R and I'm trying to create a new column which contains the mean of other 3 columns already existing. This is the code I wrote:

#Open file
All_columns<-read.csv('vCAFs_metascape_all columns.csv')
#> Warning in file(file, "rt"): cannot open file 'vCAFs_metascape_all columns.csv':
#> No such file or directory
#> Error in file(file, "rt"): cannot open the connection
datapasta::df_paste(head(All_columns,5))
#> Error in head(All_columns, 5): object 'All_columns' not found

#Selected columns
Selected_columns<-All_columns%>%
  select(logFC_1vs2,logFC_1vs3,logFC_1vs4)
#> Error in All_columns %>% select(logFC_1vs2, logFC_1vs3, logFC_1vs4): could not find function "%>%"


#Add new column
Avg_column<-mutate(Selected_columns,avg=rowMeans(select(Selected_columns,logFC_1vs2,logFC_1vs4)))
#> Error in mutate(Selected_columns, avg = rowMeans(select(Selected_columns, : could not find function "mutate"

Created on 2020-09-08 by the reprex package (v0.3.0)

Hi @JessicaP94,
Welcome to R and the RStudio community!

So that we can help you solve your problem, can you post some reproducible data? There's more information about how to do that here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners

It's great that you've shown us a photo of your data, but it will be easier for people to help you if you include a code snippet that creates some fake data that we can put directly into R and play around with to see what's not working.

Thanks!

I think you are attempting something along these lines:

library(tidyverse)

mutate(iris %>% rowwise(),
       average_length_cols = rowMeans(cbind(Petal.Length,Sepal.Length)))
1 Like

Unfortunately it seems not to work yet:
Created on 2020-09-09 by the reprex package (v0.3.0)

What seems to be the problem?

I had to use as.numeric() in the mutate() step and now it works. Thank you very much for the help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.