-8

I am trying to write a loop in R but I think the nomenclature is not correct as it does not create the new objects, here is a simplified example of what I am trying to do:

for i in (1:8) {

List_i <-List
colsToGrab_i <-grep(predefinedRegex_i, colnames(List_i$table))
List_i$table <-  List_i$table[,predefinedRegex_i]
}

I have created 'predefinedRegex'es 1:8 which the grep should use to search The loop creates an object called "List_i" and then fails to find "predefinedRegex_i".

I have tried putting quotes around the "i" and $ in front of the i , also [i] but these do not work.

Any help much appreciated. Thank you.

#

Using @RyanGrammel's answer below::

#CREATING regular expressions for grabbing sets groups 1 -7 ::::
g_1 <- "DC*"
g_2 <- "BN_._X.*"
g_3 <- "BN_a*"
g_4 <- "BN_b*"
g_5 <- "BN_a_X.*"
g_6 <- "BN_b_X.*"
g_7 <- "BN_._Y.*"

for i in (1:8) 
{
assign(x = paste("tableA_", i, sep=""), value = BigList$tableA)
assign(x = paste("Forgrep_", i, sep=""), value = colnames(get(x = paste("tableA_", i, sep=""))))
assign(x = paste("grab_", i, sep=""), value = grep((get(x = paste("g_",i, sep=""))), (get(x = paste("Forgrep_",i, sep="")))))
assign(x = paste("tableA_", i, sep=""), value = BigList$tableA[,get(x = paste("grab_",i, sep=""))])
}

This loop is repeated for each table inside "BigList". I found I could not extract columnnames from (get(x = paste("BigList_", i, "$tableA" sep=""))))

or from (get(x = paste("BigList_", i, "[[2]]" sep="")))) so it was easier to extract the tables first. I will now write a loop to repack the lists up.

jksl
  • 323
  • 5
  • 13
  • What is `grab_i`? You haven't defined it anywhere before. – Maiasaura Sep 03 '12 at 20:56
  • 1
    2 of the answers to [this question of yours](http://stackoverflow.com/questions/11118384/loop-to-perform-calculations-across-rows-on-specific-columns-matching-a-pattern) use for loops. Study the syntax. – GSee Sep 03 '12 at 20:59
  • R FAQ 7.21 may be useful: http://stackoverflow.com/questions/6034655/r-how-to-convert-string-to-variable-name ... also see http://stackoverflow.com/questions/1030532/how-can-i-access-variables-that-are-named-sequentially-by-a-loop-while-still-ins?rq=1 , which comes up first in your "related questions" sidebar list ... – Ben Bolker Sep 03 '12 at 21:09
  • @GSee thank you, I have been googling examples of for loops but I can't find examples that use lists and objects within them. I have tried [] but I will go back and read the other post. – jksl Sep 03 '12 at 21:44
  • @Ben Bolker thank you, I will read these – jksl Sep 03 '12 at 21:44
  • what is class(predefinedRegex)? – Doug Sep 03 '12 at 22:19
  • 1
    And definitely add http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?lq=1 to your favorites. – Thell Sep 03 '12 at 22:32

1 Answers1

2

Problem

Your syntax is off: you don't seem to understand how exactly R deals with variable names.

for(i in 1:10) name_i <- 1

The above code doesn't assign name_1, name_2,....,name_10. It assigns "name_i" over and over again

  • To create a list, you call 'list()', not List

  • creating a variable List_i in a loop doesn't assign List_1, List_2,...,List_8.

It repeatedly assigns an empty list to the name 'List_i'. Think about it; if R names variables in the way you tried to, it'd be equally likely to name your variables L1st_1, L2st_2...See 'Solution' for some valid R code do something similar

  • 'predefinedRegex_i' isn't interpreted as an attempt to get the variable 'predefinedRegex_1', 'predefinedRegex_2', and so one.

However, get(paste0("predefinedRegex_", i)) is interpreted in this way. Just make sure i actually has a value when using this. See below.

Solution:

In general, use this to dynamically assign variables (List_1, List_2,..)

assign(x = paste0("prefix_", i), value = i)

if i is equal to 199, then this code assigns the variable prefix_199 the value 199.

In general, use this to dynamically get the variables you assigned using the above snippet of code.

get(x = paste0("prefix_", i))

if i is equal to 199, then this code gets the variable prefix_199.

That should solve the crux of your problem; if you need any further help feel free to ask for clarification here, or contact me via my Twitter Feed.

Róisín Grannell
  • 2,048
  • 1
  • 19
  • 31
  • Thank you. You're right I am very confused about this and trying to use a loop that I think would work in BASH using $ within R, where it plainly won't. The assign function looks to be exactly what I need (do I need to use x = there?) but I am confused about creating a list using list() because I believed I was just creating a list from another list, then subsetting the dataframes inside the one I have created. It works when not in a loop. I am having a try now and will post the loop that works when it does. – jksl Sep 04 '12 at 08:16
  • Your welcome. you don't need the 'x =', I included it because I wanted to make my code clearer. In general, you never need to include parameter names if your arguments are in the right position. mean(x = 10) is equal to mean(10). It might be safer to include the parameter names though. 'List' is a valid variable name. If you had defined, say, List <- list(1:10) outside the loop then List_i <-List would be valid R code. I figured you were trying to create a new list with the function 'list()'. If you want to treat List as a variable, make sure to define it in your code. – Róisín Grannell Sep 04 '12 at 08:29
  • 2
    In general I would avoid using `assign` and `get` to create objects. If you need to create groups of variables, put them in a list. This also works much better with `apply` style loops. – Paul Hiemstra Sep 04 '12 at 09:19
  • I agree, I just figured that assign and get were closer to jksl's original intentions – Róisín Grannell Sep 04 '12 at 09:34
  • @RyanGrannell I have used assign and get, edited main post. Thank you. – jksl Sep 04 '12 at 09:59
  • looks much better, just be careful of your loop syntax. In R, loops have the format 'for(dummy_variable in list)', not 'for dummy_variable in list' – Róisín Grannell Sep 04 '12 at 11:09