1

I am a beginner in R and currently learn how to do the data wrangling job in multiple data sets. Right now I read 55 csv.file data sets with 300 rows using the following code:

Rawdata <- list.files(pattern = "*.csv")
 for(i in 1:length(Rawdata)){
  assign(Rawdata[i],read.csv(Rawdata[i], header = TRUE)[1:300])
 }

Each data set has variables "acc_X_value", "acc_Y_value", and "acc_Z_value". I failed to add a column with mutate() in these data sets. I want to show the average of these variables in a new column. Any ideas? Thank you!

1 Answers1

0

Usually it is better to keep related things in lists rather than use assign to store them in the global environment. I would do it something like this:

library(tidyverse)
Rawdata <- map(list.files(pattern = "*.csv"), read_csv)
newData <- map(rawData, mutate, average = (acc_X_value + acc_Y_value + acc_Z_value) / 3)
Ista
  • 10,139
  • 2
  • 37
  • 38
  • Thank you very much, but how do I keep all the list files in 300 rows in a data set? I tried a lot but didn't success. – Meijuan Zeng Dec 08 '18 at 20:41
  • @MeijuanZeng I'm afraid I don't understand your comment. What are you trying to do? – Ista Dec 09 '18 at 19:39