Hello! I have a character string that is a vector of variable names. I would like to use those names to access the variables and create a matrix. I tried the following:> .x[1] "mtcars$disp" "mtcars$hp" "mtcars$cyl"> .y <- NULL> for(i in 1:3) {+ .y[i] <- c(as.name(.x[[i]])) + }> .y[[1]] `mtcars$disp` [[2]] `mtcars$hp` [[3]] `mtcars$cyl` But I am having trouble converting the variables in .y into a matrix. I tried all kinds of stuff with bquote, deparse, do.call, but no good. I have a feeling that it's something simple, and I'm just not seeing it. Thanks, Erin Erin Hodgess, PhD mailto: erinm.hodgess at gmail.com [[alternative HTML version deleted]]
Richard M. Heiberger
2022-Feb-07 23:05 UTC
[R] [External] Convert a character string to variable names
> x <- c("mtcars$disp", "mtcars$hp", "mtcars$cyl") > x[1] "mtcars$disp" "mtcars$hp" "mtcars$cyl"> eval(parse(text=x))[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4> for (i in x) print(eval(parse(text=i)))[1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8 275.8 275.8 472.0 460.0 440.0 78.7 75.7 71.1 120.1 318.0 [23] 304.0 350.0 400.0 79.0 120.3 95.1 351.0 145.0 301.0 121.0 [1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52 65 97 150 150 245 175 66 91 113 264 175 335 109 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4> On Feb 07, 2022, at 17:55, Erin Hodgess <erinm.hodgess at gmail.com> wrote: > >> .x > > [1] "mtcars$disp" "mtcars$hp" "mtcars$cyl"
Hello, If you want to avoid eval(parse(.)), here is another solution. First split the strings by '$' giving the data set name in the 1st element of each list member and the column name in the 2nd. With a sapply loop find the data set name then ?get the data itself in df1. Finally, another sapply loop creates a vector of column names. And it's a matter of extracting those columns with standard '[' (or '[['. .x <- scan(text = '"mtcars$disp" "mtcars$hp" "mtcars$cyl"', what = character()) s <- strsplit(.x, "\\$") df1 <- unique(sapply(s, `[`, 1)) df1 <- get(df1, envir = .GlobalEnv) cols <- sapply(s, `[`, 2) # all columns, result is a data.frame head(df1[cols]) #> disp hp cyl #> Mazda RX4 160 110 6 #> Mazda RX4 Wag 160 110 6 #> Datsun 710 108 93 4 #> Hornet 4 Drive 258 110 6 #> Hornet Sportabout 360 175 8 #> Valiant 225 105 6 # first column in 'cols', result is a data.frame head(df1[ cols[1] ]) #> disp #> Mazda RX4 160 #> Mazda RX4 Wag 160 #> Datsun 710 108 #> Hornet 4 Drive 258 #> Hornet Sportabout 360 #> Valiant 225 # second column in 'cols', result is a vector head(df1[[ cols[2] ]]) #> [1] 110 110 93 110 175 105 Hope this helps, Rui Barradas ?s 22:55 de 07/02/2022, Erin Hodgess escreveu:> Hello! > > I have a character string that is a vector of variable names. I would like > to use those names to access the variables and create a matrix. > I tried the following: > >> .x > > [1] "mtcars$disp" "mtcars$hp" "mtcars$cyl" > >> .y <- NULL > >> for(i in 1:3) { > > + .y[i] <- c(as.name(.x[[i]])) > > + } > >> .y > > [[1]] > > `mtcars$disp` > > > [[2]] > > `mtcars$hp` > > > [[3]] > > `mtcars$cyl` > > > But I am having trouble converting the variables in .y into a matrix. > > > I tried all kinds of stuff with bquote, deparse, do.call, but no good. > > > I have a feeling that it's something simple, and I'm just not seeing it. > > > Thanks, > > Erin > > > > > Erin Hodgess, PhD > mailto: erinm.hodgess at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
"mtcars$disp" is not a variable name. "mtcars" is a variable name, and get("mtcars") will get the value of that variable assign("mtcars", ~~whatever~~) will set it. mtcars$disp is an *expression*, where $ is an indexing operator https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Indexing so what you want is> mtcars <- list(cyl=4, disp=1.8) > eval(parse(text="mtcars$disp"))[1] 1.8 Though it's easy to do this, it's very seldom a good idea. The combination of parse and eval can do ANYTHING, no matter how disastrous. Less powerful techniques are safer. Where do these strings come from in the first place? Why isn't it c("disp", "hp", "cyl")? On Tue, 8 Feb 2022 at 11:56, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Hello! > > I have a character string that is a vector of variable names. I would like > to use those names to access the variables and create a matrix. > I tried the following: > > > .x > > [1] "mtcars$disp" "mtcars$hp" "mtcars$cyl" > > > .y <- NULL > > > for(i in 1:3) { > > + .y[i] <- c(as.name(.x[[i]])) > > + } > > > .y > > [[1]] > > `mtcars$disp` > > > [[2]] > > `mtcars$hp` > > > [[3]] > > `mtcars$cyl` > > > But I am having trouble converting the variables in .y into a matrix. > > > I tried all kinds of stuff with bquote, deparse, do.call, but no good. > > > I have a feeling that it's something simple, and I'm just not seeing it. > > > Thanks, > > Erin > > > > > Erin Hodgess, PhD > mailto: erinm.hodgess at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]