Lorenzo Ramirez
Lorenzo Ramirez

Reputation: 13

Trouble combining columns into one vector

I am trying to combine the same named column from n csv sheets into one vector for which I can then ask for the mean of the vector and get one value.

Instead it seems I get a vector of numeric vectors (let's call it Lo). So when I call the mean function on Lo, I get a mean for every of the individual columns.

Below is the code that responds as described in (1)

p <- list.files(getwd(), full.names = TRUE)[1:10] 
Re <- c()

for(i in p) {
  Lo <- read.csv(i, header = TRUE)
  Zo <- na.omit(Lo$sulfate)
  An <- c(Re, Zo)
  print(mean(An)
}

I do not get error messages. I should only be getting one mean value, #but instead I get a mean for each sheet's $sulfate column.

#[1] 3.880701
#[1] 4.460811
#[1] 4.327613
#[1] 4.214956
#[1] 4.210072
#[1] 4.102132
#[1] 3.820059
#[1] 4.781354
#[1] 3.645644
#[1] 0.6243649

Upvotes: 1

Views: 288

Answers (1)

heds1
heds1

Reputation: 3448

Not totally sure this is what you're looking for, but something like this?

p <- list.files(getwd(), full.names = TRUE)[1:10]
Re <- c()
vector_list <- list()
for(i in p) {
    Lo <- read.csv(i, header = TRUE)
    Zo <- na.omit(Lo$sulfate)
    An <- c(Re, Zo)
    vector_list[[i]] <- An
    }

mean(unlist(vector_list))

Upvotes: 1

Related Questions