Re: [R] Create macro_var in R

2016-02-05 Thread Jeff Newmiller
You REALLY NEED to read the "Introduction to R" document discussion of indexing. tab is a variable. It is apparently a data.frame. tab[[mvar]] is an expression that retrieves part of the data in the tab data.frame. The data it returns is a vector, not a data.frame. The "[[" operator extracts

Re: [R] Create macro_var in R

2016-02-05 Thread William Dunlap via R-help
If 'tab' is a data.frame then new.tab <- tab[[mvar]] is a column from that data.frame, not a data.frame with one column. new.tab <- tab[ , mvar, drop=FALSE ] will give you a data.frame that you can add to with either of nvar <- "newName" new.tab[ , nvar] <- newColumn new.tab[[nvar]] <-

Re: [R] Create macro_var in R

2016-02-05 Thread MacQueen, Don
Yes, you can use a name stored in a variable to create a new column in a data frame (guessing that's what you want). Here's an example: > df <- data.frame(a=1:5) > df[['b']] <- 2:6 > df a b 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 > mvar <- 'c' > df[[mvar]] <- 0:4 > df a b c 1 1 2 0 2 2 3 1 3 3 4 2 4 4 5

Re: [R] Create macro_var in R

2016-02-03 Thread Gabor Grothendieck
See Example 5. Insert Variables on the sqldf home page. https://github.com/ggrothendieck/sqldf On Wed, Feb 3, 2016 at 2:16 PM, Amoy Yang via R-help wrote: > First, MVAR<-c("population) should be the same as "population'". Correct? > You use tab[[MVAR]] to refer to "population" where doub

Re: [R] Create macro_var in R

2016-02-03 Thread David Winsemius
> On Feb 3, 2016, at 11:57 AM, Amoy Yang via R-help > wrote: > > Right! the following works to r but not sqldf. > MVAR <- "population" > tab[[ MVAR ]] > sqldf("select tab[[MVAR]] from tab") If ypu need a string to pass to sqldf, then use the `paste` function to build a single string: sqldf(

Re: [R] Create macro_var in R

2016-02-03 Thread Amoy Yang via R-help
Right! the following works to r but not sqldf. MVAR <- "population" tab[[ MVAR ]] sqldf("select tab[[MVAR]] from tab") On Wednesday, February 3, 2016 1:18 PM, Amoy Yang via R-help wrote: First, MVAR<-c("population) should be the same as "population'". Correct? You use tab[[MVAR]] to re

Re: [R] Create macro_var in R

2016-02-03 Thread MacQueen, Don
Repeat many times: R does not work the same way as SAS, do not expect it to If I needed to construct an SQL statement in which the name of a field is provided by the value of another variable, I would consider this: key <- 'pop' sql.stmt <- paste("select grade, count(*) as cnt, min(",key,") a

Re: [R] Create macro_var in R

2016-02-03 Thread Amoy Yang via R-help
First, MVAR<-c("population) should be the same as "population'". Correct? You use tab[[MVAR]] to refer to "population" where double [[...]] removes double quotes "...", which seemingly work for r-code although it is tedious in comparison direct application in SAS %let MVAR=population. But it does

Re: [R] Create macro_var in R

2016-02-03 Thread Jeff Newmiller
Are you perhaps needing to (re-)read the discussion on indexing in the "Introduction to R" document that comes with the software? (That is a common deficiency...) It looks to me like you want something like MVAR <- "population" tab[[ MVAR ]] -- Sent from my phone. Please excuse my brevity. O

Re: [R] Create macro_var in R

2016-02-03 Thread Bert Gunter
I am not sure what you want, but: 1) If you have not already done so, go through an R tutorial or two. For some suggestions: https://www.rstudio.com/resources/training/online-learning/#R R is quite different than SAS. 2) If I misunderstand, perhaps ?within is what you are looking for. Cheer

Re: [R] Create macro_var in R

2016-02-03 Thread ruipbarradas
Hello, You can't use tab$MVAR but you can use tab[[MVAR]] if you do MVAR <- "population" (no need for c()). Hope this helps, Rui Barradas   Citando Amoy Yang via R-help : > population is the field-name in data-file (say, tab). > MVAR<-population takes data (in the column of population) rat

Re: [R] Create macro_var in R

2016-02-03 Thread Duncan Murdoch
On 03/02/2016 1:23 PM, Amoy Yang wrote: population is the field-name in data-file (say, tab). MVAR<-population takes data (in the column of population) rather than field-name as done in SAS: %let MVAR=population; In the following r-program, for instance, I cannot use ... tab$MVAR...or simply M

Re: [R] Create macro_var in R

2016-02-03 Thread Amoy Yang via R-help
population is the field-name in data-file (say, tab). MVAR<-population takes data (in the column of population) rather than field-name as done in SAS:  %let MVAR=population; In the following r-program, for instance, I cannot use ... tab$MVAR...or simply MVAR itself since MVAR is defined as "popu

Re: [R] Create macro_var in R

2016-02-03 Thread Duncan Murdoch
On 03/02/2016 12:41 PM, Amoy Yang via R-help wrote: There is a %LET statement in SAS: %let MVAR=population; Thus, MVAR can be used through entire program. In R, I tried MAVR<-c("population"). The problem is that MAVR comes with double quote "" that I don't need. But MVAR<-c(population) di

Re: [R] Create macro_var in R

2016-02-03 Thread Henrik Bengtsson
Don't know what `population` is, but a simple assignment MVAR <- population may provide what you need. Note, no c(). For example, > foo <- rnorm > foo(3) [1] -0.08093862 -0.87827617 1.52826914 /Henrik On Wed, Feb 3, 2016 at 9:41 AM, Amoy Yang via R-help wrote: > There is a %LET statement