I am trying to read the multiple dataframes using the for loops and am trying to assign the variable names in the data frames. I want to give the same column name to all the dataframes.
The for loop I used is as follows:
for (i in 1:15){
assign(paste0("watersurf",i),read.table(paste0("D:/water-surf/","water-surf-",i,".dat"),header=F,skip=1,sep=""))
}
By using the above code I was able to create the 15 data frames. How can I assign the names to all the data frames ? I used the following code:
for (i in 1:15){
assign(paste0("watersurf",i),read.table(paste0("D:/water-surf/","water-surf-",i,".dat"),header=F,skip=1,sep=""))
names(paste0("watersurf",i))<- c("X","Y","Z")
}
I got the error as follows:
Error in names(paste("watersurf", i)) <- c("X", "Y", "Z") :
target of assignment expands to non-language object
Can anyone suggest how can I get rid of the above error or any efficient way to rename the columns in all the dataframes at once. Of course I could try to assign names individually, but I am looking for a simple and elegant solution.
The sample data of one csv file I am working on is as follows:
* , Water Levels, Depth Averaged
407902.775 3437067.475 0.331
406719.675 3436930.750 0.327
405754.025 3436197.650 0.325
404614.375 3435972.650 0.322
404273.050 3434775.450 0.321
403347.425 3433981.900 0.320
404118.025 3433179.975 0.318
404798.100 3432441.075 0.317
403701.000 3432521.400 0.317
402682.750 3432727.350 0.316
402835.400 3431561.150 0.315
403957.775 3431203.900 0.313
405159.775 3431120.150 0.309
405918.250 3430149.075 0.307
405902.700 3428908.500 0.306
405051.075 3428616.450 0.306
404963.950 3427407.225 0.304
405570.675 3426347.950 0.304
406407.800 3425479.275 0.304
406799.425 3424354.200 0.303
407590.800 3423501.200 0.302
408119.375 3422490.975 0.301
408033.050 3421648.575 0.300
407924.425 3420566.700 0.300
paste0("watersurf", i)returns a string.