3

I am working with a bunch of currency data .csv files. These .csv come without a header, which I am trying add, using the colnames function.

colnames(variable_name) <- c('Date', 'Time', 'Open', 'Close', 'Volume')

The data import and the assignment of the column headers is supposed to be done automatically using a for loop. The name of the data frame is part of the file name.

file_names <- list.files()

for (i in 1:length(file_names)){
    assign(substr(file_names,1,6)[i], read.csv(file_names[i], header=F))
    colnames(variable_name) <- c('Date', 'Time', 'Open', 'Close', 'Volume')
}

How can I manage to input the variable_name into the colnames function. I tried using:

colnames(substr(file_names,1,6)[i])

But that would give me the input "AUDUSD", and I need to input AUDUSD without the quotation marks.

So how can I manage to convert the String into a variable name I can use here? Or maybe my approach is completly wrong here?

Thanks alot!

Chris

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
user2102383
  • 43
  • 1
  • 1
  • 3
  • 3
    @Joshua: wrong duplicate. The OP is looking for `get`, you pointed him to `assign`. – flodel Feb 23 '13 at 14:08
  • 5
    @flodel: fair enough, here's a [`get` duplicate](http://stackoverflow.com/q/10429829/271616). Do we really need to re-answer questions that are in the Examples of the R documentation (`?assign` in this case) and the R FAQ (7.21)? – Joshua Ulrich Feb 23 '13 at 14:35

3 Answers3

8

You were looking for get. Your code would look like this:

file_names  <- list.files()
short_names <- substr(file_names, 1, 6)

for (i in seq_along(file_names)) {
    assign(short_names[i], read.csv(file_names[i], header = FALSE))
    colnames(get(short_names[i])) <- c('Date', 'Time', 'Open', 'Close', 'Volume')
}

but it seems easier to use the col.names option from the read.* functions, try:

assign(short_names[i], read.csv(file_names[i], header = FALSE,
                                col.names = c('Date', 'Time', 'Open',
                                              'Close', 'Volume'))

and if you are not familiar with the *apply family of functions, your whole loop can be replaced with:

mapply(assign, short_names, lapply(file_names, read.csv, header = FALSE,
                                   col.names = c('Date', 'Time', 'Open',
                                                 'Close', 'Volume'))
flodel
  • 87,577
  • 21
  • 185
  • 223
  • I don't think `names(get(x))` works though; there's a question that asks about it but I can't find it right now. The error is `target of assignment expands to non-language object`. In any case, the `col.names` option is the right way to do this. – Aaron left Stack Overflow Feb 23 '13 at 14:21
3

I recommend that you use a list. That way, things get nice and clean:

file_names <- list.files()

data <- lapply (file_names, read.csv, header = FALSE)
names (data) <- substr(file_names, 1, 6)  # now you can access data$AUDUSD

## colnames for all data.frames
data <- lapply (data, `colnames<-`, c('Date', 'Time', 'Open', 'Close', 'Volume'))

even easier:

data <- lapply (file_names, read.csv, header = FALSE, 
                col.names = c ('Date', 'Time', 'Open', 'Close', 'Volume'))
names (data) <- substr (file_names, 1, 6)  # now you can access data$AUDUSD

(Personally, I'd put all those data.frames into one, with an additional column with the $conversion or so)

However, you can of course have every data.frame in its own variable. In that case, the get needs a bit care:

> colnames (get (variable_name)) <- c('Date', 'Time', 'Open', 'Close', 'Volume')
error: "target of assignment expands to non-language object")  

(the error message is a back-translated)

This works, though:

    tmp <- get (variable_name) # in your case, rather do: tmp <- read.csv (...)
    colnames (tmp) <- letters [1:2]
    assign (variable_name, tmp)

(I'd put the colnames<- before the first assign, anyways)

cbeleites unhappy with SX
  • 13,717
  • 5
  • 45
  • 57
  • Thank you for pointing me to this approach! As I'm new to R answers like this will point me in the right direction. – user2102383 Feb 23 '13 at 15:19
0

I don't think you're getting what you think you're getting. Here's my test case:

Rgames> foo[1:6]
[1] "aggfrac.R"     "AJtranslate.c" "AJtranslate.R" "anaclock.R"   
[5] "apollo.R"      "askrm.R"      
Rgames> bar<-matrix(nr=2,nc=5)
Rgames> colnames(bar)<-substr(foo,1,6)[1:5]
Rgames> bar
     aggfra AJtran AJtran anaclo apollo
[1,]     NA     NA     NA     NA     NA
[2,]     NA     NA     NA     NA     NA

You can see that the column names are 'just right.'

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73