Øystein Myrland
Øystein Myrland

Reputation: 93

Read data frames from a function in R

The following two lines will read a data frame, and assign column names:

br <-
read.table("http://www.principlesofeconometrics.com/poe4/data/dat/br.dat")

colnames(br) <- paste(lapply(read.table("http://www.principlesofeconometrics.com/poe4/data/def/br.def", skip=2, nrows=1), as.character, sep=","))

I would like to create a function that reads the file and assigns column names based on the file name, "br" in this case. I have tried:

poe4read <- function(x) {
x <- read.table(gsub("x", "br",    "http://www.principlesofeconometrics.com/poe4/data/dat/x.dat"))
colnames(x) = paste(lapply(read.table(gsub("x", "br",    "http://www.principlesofeconometrics.com/poe4/data/def/x.def"),
                                   skip=2, nrows=1), as.character, sep=","))}

This does not return a data frame "br", but it works line by line, creating the data frame "x". Any suggestions?

Upvotes: 0

Views: 104

Answers (2)

If I understood well your question, this should do the job :

poe4read <- function(x){
        fileName <- as.character(x)
        url01 <- paste0("http://www.principlesofeconometrics.com/poe4/data/dat/", 
                        fileName, ".dat")
        url02 <- paste0("http://www.principlesofeconometrics.com/poe4/data/def/", 
                        fileName, ".def")
        x <- read.table(url01, header=FALSE)

        # fetch colnames in the second file
        # and modify column names according to the file name :
        colnames(x) <- paste0(fileName, 
                              paste(lapply(read.table(url02, skip=2, nrows=1),
                              as.character, sep=",")))
        x
      }

Result :

br <- poe4read("br")
head(br,3)

  brprice brsqft brBedrooms brBaths brAge brOccupancy brPool brStyle 
1   66500    741          1       1    18           1      1       1 
2   66000    741          1       1    18           2      1       1 
3   68500    790          1       1    18           1      0       1 
  brFireplace brWaterfront brDOM 
1           1            0     6 
2           0            0    23 
3           1            0     8 

When you're calling the function, you need to use quotes around the file name for it to work. poe4read("br").

You should then be able to use it on multiple file adding the name of the specific file at the beginning of each column.

Upvotes: 0

Tensibai
Tensibai

Reputation: 15784

I would do it this way.

poe4read <- function(x) {
  x <- read.table(paste0("http://www.principlesofeconometrics.com/poe4/data/dat/",x,".dat")))
  colnames(x) = paste(lapply(read.table(paste0("http://www.principlesofeconometrics.com/poe4/data/def/",x,".def")), skip=2, nrows=1), as.character, sep=","))}

br <- poe4read("br")

Variable are scoped, creating a global variable within a function sounds a bad idea but if you really want to you can do it like this:

a <- function(var) {
  assign( var, data.frame(c(1,0)), envir=.GlobalEnv) # Warning this will always create the variable in the gloabl environment
  assign( var, setNames( get(var), "test"), envir=.GlobalEnv)
}

We have to use setNames which return the modified object instead of modifying it directly inside another assign to work on the global variable.

Call

> a("mytest")

Output:

> mytest
  test
1    1
2    0 

Upvotes: 1

Related Questions