1

I am trying to write my first loop in R. I have a database loaded in the global environment in R-studio containing a set of tables. Here is an example of one table.

Initial

I would like to make the first column in each file the row name like this:

Edited

I have tried a couple of ideas but I am having no luck. First code:

tables = ls(envir=.GlobalEnv)[sapply(ls(envir=.GlobalEnv), 

for (i in 1:length(tables)){
df=read.table(tables[[i]], sep="\t", header=T); rownames(df) = df[,1]}
}

Second code:

tables = ls(envir=.GlobalEnv)[sapply(ls(envir=.GlobalEnv), 

for(i in 1:length(tables)){
tables[[i]] <- tables[[i]][,-1]
rownames(tables[[i]]) <- tables[[i]][,1]}
}

I'm sure it's an easy solution, I am still a novice at R though. Any thoughts on how to get this to work?

surajs1n
  • 1,493
  • 6
  • 23
  • 34
  • 1
    Please don't post pictures of data. It's better to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in the question itself. Also there seems to be code missing in your `tables=` creation. There is no closing "]". – MrFlick Mar 29 '17 at 15:37
  • Please make sure your code works as written, and use text not images for the data. Meanwhile, it looks like your second attempt might work if you just swapped the two lines in the for loop around. –  Mar 29 '17 at 15:38
  • Instead of using a for-loop, you could also use: `lapply(tables, function(x) as.data.frame(x, row.names = x[[1]])[, -1, drop = FALSE] )` – Jaap Mar 29 '17 at 16:01

1 Answers1

0

Your second attempt is actually correct but you are dropping the column you want to use for the names before you make change the row names!

Importantly, you should use the drop = FALSE in the deselection of the first row, to keep the data frame type. See what may happen and why to use that statement.

One way of getting a list of all data frames in the global environment is to check which variables are data frames and using the get function to retrieve it by names.

  ## Logical vector of which variables from ls() are of type data.frame 
temp <- sapply(ls(), function(i) is.data.frame(get(i)))
  ## Combine them all in a list
tables <- lapply(names(temp)[temp],get)

tables ## Example of have
## [[1]]
##   a b d
## 1 9 3 a
## 2 8 4 b
## 
## [[2]]
##   b c
## 1 5 2
## 2 4 3
## 3 3 4
## 4 2 5

To get the output we want is just a slight modification of your code as mentioned above.

for(i in 1:length(tables)){
    rownames(tables[[i]]) <- tables[[i]][,1]
    tables[[i]] <- tables[[i]][,-1,drop = FALSE]
}
tables ## Example of want
## [[1]]
##   b d
## 9 3 a
## 8 4 b
## 
## [[2]]
##   c
## 5 2
## 4 3
## 3 4
## 2 5
Community
  • 1
  • 1
Therkel
  • 1,379
  • 1
  • 16
  • 31
  • Thanks! I realize now that I had the two lines of code switched. However, now when I run the code I get the following error. "Error in tables[[i]][, 1] : incorrect number of dimensions" – Marshall Clinesmith Mar 29 '17 at 16:00
  • Can you try to paste the return of `str(tables)`? – Therkel Mar 29 '17 at 16:03
  • Can you please check if the creation of `tables` with the code supplied at the top of my edited answer corrects the code? I think what you have been doing this far is grabbing *all* variables from the Global Environment into a list, instead of only variables of type `data.frame`. – Therkel Mar 29 '17 at 16:23
  • (I have also implicitly assumed that your table entries are `data.frame`s) – Therkel Mar 29 '17 at 16:24
  • By the output of the `str` statement, it seems that your `tables` variable was just the names. – Therkel Mar 29 '17 at 16:24
  • The edit you suggested seems to have fixed the issue with the tables variable. Now the tables variable is a list of classes DB.table and data.frame categorized under Values. When I run the code for changing the row names now it seems to correctly change them under the table variable within the environment. How would i then proceed to have it change each data file within the environment. Is there a way to split the data by the original name of each file? – Marshall Clinesmith Mar 29 '17 at 20:52
  • Yeah, sure @MarshallClinesmith but I believe that is a different question. :) Have a look at [`?assign`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/assign.html) and if you have difficulties finding help, ask a new question! I do believe it has been asked before, however. – Therkel Mar 30 '17 at 11:54