Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R read.csv loop with iterating variable in quotation marks

$
0
0

This is my code:

library(data.table)
library(stringr)
parameters <- c("conductivity","calcium","chloride","magnesium","phosphate","potassium","salinity","sodium","sulphate")
for (i in parameters){
  i <- read.csv(str_c("./Data/Parameters/",i,".csv"), sep=",", header=FALSE)
  i <- unique(i)
  i <- subset(i, select=c(1,2,4,6))
  i <- setnames(i, c("site","date", str_c("",i,""), "material"))
  i[,3] <- as.numeric(i[,3])
  i <- subset(i, i > 0)
}

Now, there are two things that don't work here. The first is in the setnames function: it doesn't understand that it needs to label one of the columns of the CSV with the variable name. The second is that it doesn't actually call the imported files with 'conductivity', 'calcium', etc but simply calls them all 'i'. How can I fix this?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>