r - Reading multiple csv files as new dataframes and renaming the columns -
i trying read multiple dataframes using loops , trying assign variable names in data frames. want give same column name dataframes.
the loop used 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 above code able create 15 data frames. how can assign names data frames ? used 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 error follows:
error in names(paste("watersurf", i)) <- c("x", "y", "z") : target of assignment expands non-language object
can suggest how can rid of above error or efficient way rename columns in dataframes @ once. of course try assign names individually, looking simple , elegant solution.
the sample data of 1 csv file working on 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
until comes better solution, change first loop to:
for (i in 1:15){ data <- read.table(paste0("d:/water-surf/","water-surf-",i,".dat"),header=f,skip=1)) names(data) <- c("x", "y", "z") assign(paste0("watersurf", i), data) }
Comments
Post a Comment