0

I want to read a file and calculate the mean of it.

`>list
[1] "book1.csv" "book2.csv".

for book1

observation1
    23
    24
    65
    76
    34

In books i have a variable observation 1 and observation 2 column for book 1 and 2 respectively. So i want to write a function where i can calculate mean of it.I am new to R and not able subset the variable of books. Can anyone please help me out in writing the function?

Lamyanba
  • 31
  • 1
  • 8

1 Answers1

1

Try this. File represents the file to be read in (book1) and the variable represents the variable to take mean over (observation 1)

read.mean<-function(file,variable){
  df<-read.csv(file)
  mean.df <- mean(df[,variable])
  return(mean.df)
}

Make sure to pass your arguments in quotes, i.e. read.mean("book1", "observation1"). There is a way to do it without the quotes (Passing a variable name to a function in R) but it is complicated.

Community
  • 1
  • 1
Jacob H
  • 4,317
  • 2
  • 32
  • 39