0

I have several vectors named: aa, bb, ab, ac, etc. I'd like to use their names ("01","02","03","04", etc) to match the vectors with a df. But I get an error because names(i) doesn't work, given that (in case i <- "aa") R reads names("aa"), instead of: names(aa). How can be solved?

noquote(i) worked, but not inside names: names(noquote(i)). This produced an error.

x <- c("aa","bb","ab","ac")
names(x) <- c("01","02","03","04")

for (i in x) {
  df[match(names(i), df$ID), i] <- noquote(i)
}

########################################

Example:

df <- data.frame(ID = c("01","02","02","03","03","03"), 
                 var_1 = c(0,0,1,0,0,1),
                 var_2 = c(0,0,0,0,1,0),
                 var_3 = c(1,0,0,0,0,0))

aa <- tapply(df$var_1, df$ID, max, na.rm = T)
bb <- tapply(df$var_2, df$ID, max, na.rm = T)
cc <- tapply(df$var_3, df$ID, max, na.rm = T)

In df_2 there's just one rown for each ID and I want to include in df_2 the vectors I created with tapply.

df_2 <- data.frame(ID=c("01","02","03"), age=c(34,12,49))

In order to match exactly the ID I used the following code:

df_2[match(names(aa), df_2$ID), "aa"] <- aa
df_2[match(names(bb), df_2$ID), "bb"] <- bb
df_2[match(names(cc), df_2$ID), "cc"] <- cc

In order to avoid this repetition of code I was trying to use this code (that i wrote at the beginning of the post):

x <- c("aa","bb","cc")
    
    for (i in x) {
      df_2[match(names(i), df_2$ID), i] <- noquote(i)
    }

but I have a problem with names(i)

jeff
  • 323
  • 1
  • 7
  • I have a feeling the solution is simple, but can't be sure without a reproducible example of the `df`. Could you provide that to maximize chances of getting an answer? – csgroen Apr 01 '21 at 14:31
  • What are you trying to define with: names(x) <- c("01","02","03","04")? Can you please try to clarify the question? – Chelmy88 Apr 01 '21 at 14:32
  • You should [use a list of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207). (Or lists in general) – Gregor Thomas Apr 01 '21 at 14:37
  • If you make your example fully reproducible (share sample `aa`, `bb` vectors and a small sample `df`), I'd be happy to demonstrate. – Gregor Thomas Apr 01 '21 at 14:46
  • I added my post with an example – jeff Apr 01 '21 at 14:52
  • I find the logic still very confusing. What is your desired output? A df called `df_2` that has the information on `aa`, `bb` and `cc`? – csgroen Apr 01 '21 at 15:27
  • Yes, it is right. Unfortunately with just a small example it seems not logic. – jeff Apr 01 '21 at 15:28
  • I edited my post because there was a mistake – jeff Apr 01 '21 at 16:16
  • `names` will get you only the names iof `x`, not the names of the items extracted by `in`. If you wnat to index with numers use `for( i in seq_along(x)){.}` and work with `x[i]` or `names(x)[i]` in your code. And if you have a character vector , say `char="aa"` and need to pull in values from the symbol table of your workspace, then use `get(char)` which will return the value of the data object named `aa`. – IRTFM Apr 01 '21 at 20:53
  • 1
    Like the other commenters here, I can't figure out what you're trying to achieve. I would recommend providing your expected output. – Ian Campbell Apr 02 '21 at 19:04

0 Answers0