0

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
3
  • It's not working because paste0("watersurf", i) returns a string. Commented Aug 19, 2013 at 17:44
  • @SeñorO : Thank you for clarifying this. Is there any other way to change the colnames ? Commented Aug 19, 2013 at 17:47
  • I don't know of any, although there may be some. Maybe your best bet is doing that in the first loop, where you (1) read the data to a temp. object, (2) change the colnames, (3) assign the object name. Commented Aug 19, 2013 at 17:52

2 Answers 2

2

Until someone comes up with a better solution, change the 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)
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand your question, you're trying to merge multiple text files into a single data frame and then rename the columns. You can do this easily using plyr

require(plyr)
# set working directory
setwd("D:/water-surf/")

# check that works for 1 file
table_1 <- read.table("./water-surf-1.dat", header= F, skip= 1, sep= "")

# import all files
files <- dir("D:/water-surf/") # declare the file directory
table_all <- ldply(files, read.table, stringsAsFactors= TRUE, header= F, 
  skip= 1, sep= "")

# rename the columns
names(table_all) <- c("var_1", "var_2", "var_3")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.