loops - Looping in R and Referring to Variable Names - Stack Overflow

admin2025-05-01  0

I am a fairly new user to R and am trying to reference variables via a loop list. I have tried the following but am getting object errors. It seems that as i is looping, it is not being utilized where [i] is coded. Any help with this syntax?

month_list = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct")

for (i in month_list){
  seph_raw_[i]$prov <- ifelse(seph_raw_[i]$geoval == "10", "NF",
                       ifelse(seph_raw_[i]$geoval == "11", "PE",
                       ifelse(seph_raw_[i]$geoval == "12", "NS",
                       ifelse(seph_raw_[i]$geoval == "13", "NB",
                       ifelse(seph_raw_[i]$geoval == "24", "QC",
                       ifelse(seph_raw_[i]$geoval == "35", "ON",
                       ifelse(seph_raw_[i]$geoval == "46", "MB",
                       ifelse(seph_raw_[i]$geoval == "47", "SK",
                       ifelse(seph_raw_[i]$geoval == "48", "AB",
                       ifelse(seph_raw_[i]$geoval == "59", "BC",
                       ifelse(seph_raw_[i]$geoval == "60", "YT",
                       ifelse(seph_raw_[i]$geoval == "61", "NT",
                       ifelse(seph_raw_[i]$geoval == "62", "NU",
                       "Other")))))))))))))

}

I was expecting seph_raw_jan to be used as per the loop, along with the other months.

Specifically, I wanted to used the variable i as part of the object name for the separate monthly dataframes, but comments below have stated that this is not possible. For example, when i was at the jan part, I wanted jan to be populated everywhere you see [i] in the code. But it seems this is not possible.

I am a fairly new user to R and am trying to reference variables via a loop list. I have tried the following but am getting object errors. It seems that as i is looping, it is not being utilized where [i] is coded. Any help with this syntax?

month_list = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct")

for (i in month_list){
  seph_raw_[i]$prov <- ifelse(seph_raw_[i]$geoval == "10", "NF",
                       ifelse(seph_raw_[i]$geoval == "11", "PE",
                       ifelse(seph_raw_[i]$geoval == "12", "NS",
                       ifelse(seph_raw_[i]$geoval == "13", "NB",
                       ifelse(seph_raw_[i]$geoval == "24", "QC",
                       ifelse(seph_raw_[i]$geoval == "35", "ON",
                       ifelse(seph_raw_[i]$geoval == "46", "MB",
                       ifelse(seph_raw_[i]$geoval == "47", "SK",
                       ifelse(seph_raw_[i]$geoval == "48", "AB",
                       ifelse(seph_raw_[i]$geoval == "59", "BC",
                       ifelse(seph_raw_[i]$geoval == "60", "YT",
                       ifelse(seph_raw_[i]$geoval == "61", "NT",
                       ifelse(seph_raw_[i]$geoval == "62", "NU",
                       "Other")))))))))))))

}

I was expecting seph_raw_jan to be used as per the loop, along with the other months.

Specifically, I wanted to used the variable i as part of the object name for the separate monthly dataframes, but comments below have stated that this is not possible. For example, when i was at the jan part, I wanted jan to be populated everywhere you see [i] in the code. But it seems this is not possible.

Share Improve this question edited Jan 3 at 16:42 M-- 29.8k10 gold badges70 silver badges106 bronze badges asked Jan 2 at 20:03 Signals613Signals613 212 bronze badges 3
  • 2 Please provide a complete self contained example so that others can run the code. Currently seph_raw_ is missing. Also please clarify what the question is. Also clarify by showing what you got and what you expected. Simplify the question if necessary so an answer to it still gives the idea of how to fix the original problem. – G. Grothendieck Commented Jan 2 at 20:09
  • 3 You can't use a variable like i as a piece of an object name like this. (Well, you can if you paste() the object name together and use get() to access it and <-( to assign to it... it's bug-prone and hard to read.) Generally the good way to do something like this is with lists of data frames. (Or one big data frame.) Have a look at my answer at How to make a list of data frames? for some discussion and examples. – Gregor Thomas Commented Jan 2 at 20:19
  • 2 You also might want to look at using dplyr's functions case_when() or case_match() which would save a lot of typing compared to a bunch of ifelses. – Gregor Thomas Commented Jan 2 at 20:20
Add a comment  | 

1 Answer 1

Reset to default 5

There are a couple of issues here, and the core of it may be misunderstanding that the brackets in R ([]) index something, but dont append that text to a variable. For instance, with the vector xx <- c("A", "V", "K"), calling xx[3] will return "K" as it is the 3rd position. But does not call or create something like xxK.

If you have data like this for each month (here, January):

seph_raw_jan <- data.frame(geoval = c(1, 11, 12),
                           other_var = LETTERS[1:3])
seph_raw_jan
#   geoval other_var
# 1      1         A
# 2     11         B
# 3     12         C

I would (serendipitously) suggest replacing all of the numerical values in geoval by using indexing, not a loop. To do this, you could first create a reference list, here called prov_nms:

prov_nms <- c("10" = "NF",
              "11" = "PE",
              "12" = "NS", 
              "13" = "NB", 
              "24" = "QC", 
              "35" = "ON", 
              "46" = "MB",
              "47" = "SK", 
              "48" = "AB", 
              "59" = "BC", 
              "60" = "YT", 
              "61" = "NT", 
              "62" = "NU")

Then just index on prov_nms:

seph_raw_jan$prov <- prov_nms[as.character(seph_raw_jan$geoval)]

#   geoval other_var prov
# 1      1         A <NA>
# 2     11         B   PE
# 3     12         C   NS

Note that if a value is not in the reference data, it returns NA.

If you have all twelve months as separate data frames, a good practice would be to combine them in a list and can then use lapply (here, only using Jan and Feb as an example):

# January
seph_raw_jan <- data.frame(geoval = c(1, 11, 12),
                           other_var = LETTERS[1:3])
# February
seph_raw_feb <- data.frame(geoval = c(13, 10, 24),
                           other_var = LETTERS[4:6])
# Combined list
mlist <- list(seph_raw_jan, seph_raw_feb)

ll <- lapply(mlist, \(x){
  x$prov <- prov_nms[as.character(x$geoval)];
  x
})

# [[1]]
#   geoval other_var prov
# 1      1         A <NA>
# 2     11         B   PE
# 3     12         C   NS
# 
# [[2]]
#   geoval other_var prov
# 1     13         D   NB
# 2     10         E   NF
# 3     24         F   QC

转载请注明原文地址:http://anycun.com/QandA/1746099007a91651.html