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 "population" with double quotes if using MVAR<-c("population") On Wednesday, February 3, 2016 11:54 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote: 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) did NOT work out. Any way that double quote can be removed as done in SAS when creating macro_var? > Thanks in advance for helps!R doesn't have a macro language, and you usually don't need one. If you are only reading the value of population, then MAVR <- population is fine.? This is sometimes the same as c(population), but in general it's different:? c() will remove some attributes, such as the dimensions on arrays. If you need to modify it in your program, it's likely more complicated.? The normal way to go would be to put your code in a function, and have it return the modified version.? For example, population <- doModifications(population) where doModifications is a function with a definition like doModifications <- function(MAVR) { ? ? # do all your calculations on MAVR ? ? # then return it at the end using ? ? MAVR } Duncan Murdoch [[alternative HTML version deleted]]
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 MVAR itself since MVAR is defined as "population" with double quotes if using MVAR<-c("population")In that case, you can use MVAR <- "population" and then work with tab[[MVAR]] instead of tab$population. But it would often make more sense to extract that column into a separate variable, and work with that, either as MVAR <- tab$population or tab$population <- doModifications(tab$population) following the same patterns as below. Duncan Murdoch> > On Wednesday, February 3, 2016 11:54 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > > > 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) did NOT work out. Any way that double quote can be removed as done in SAS when creating macro_var? > > Thanks in advance for helps! > > R doesn't have a macro language, and you usually don't need one. > > If you are only reading the value of population, then > > MAVR <- population > > is fine. This is sometimes the same as c(population), but in general > it's different: c() will remove some attributes, such as > the dimensions on arrays. > > If you need to modify it in your program, it's likely more complicated. > The normal way to go would be to put your code in a function, and have > it return the modified version. For example, > > population <- doModifications(population) > > where doModifications is a function with a definition like > > doModifications <- function(MAVR) { > # do all your calculations on MAVR > # then return it at the end using > MAVR > } > > Duncan Murdoch > > >
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. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Feb 3, 2016 at 10:23 AM, Amoy Yang via R-help <r-help at r-project.org> 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 MVAR itself since MVAR is defined as "population" with double quotes if using MVAR<-c("population") > > On Wednesday, February 3, 2016 11:54 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > > > 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) did NOT work out. Any way that double quote can be removed as done in SAS when creating macro_var? >> Thanks in advance for helps! > > R doesn't have a macro language, and you usually don't need one. > > If you are only reading the value of population, then > > MAVR <- population > > is fine. This is sometimes the same as c(population), but in general > it's different: c() will remove some attributes, such as > the dimensions on arrays. > > If you need to modify it in your program, it's likely more complicated. > The normal way to go would be to put your code in a function, and have > it return the modified version. For example, > > population <- doModifications(population) > > where doModifications is a function with a definition like > > doModifications <- function(MAVR) { > # do all your calculations on MAVR > # then return it at the end using > MAVR > } > > Duncan Murdoch > > > > [[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.
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 <r-help at r-project.org>:> 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 > "population" with double quotes if using MVAR<-c("population") > > ? ?On Wednesday, February 3, 2016 11:54 AM, Duncan Murdoch > <murdoch.duncan at gmail.com> wrote: > > 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) >> did NOT work out. Any way that double quote can be removed as done >> in SAS when creating macro_var? >> Thanks in advance for helps! > > R doesn't have a macro language, and you usually don't need one. > > If you are only reading the value of population, then > > MAVR <- population > > is fine.? This is sometimes the same as c(population), but in general > it's different:? c() will remove some attributes, such as > the dimensions on arrays. > > If you need to modify it in your program, it's likely more complicated.? > The normal way to go would be to put your code in a function, and have > it return the modified version.? For example, > > population <- doModifications(population) > > where doModifications is a function with a definition like > > doModifications <- function(MAVR) { > ? ? # do all your calculations on MAVR > ? ? # then return it at the end using > ? ? MAVR > } > > Duncan Murdoch > > ? ? ? ? [[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.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]
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. On February 3, 2016 10:23:12 AM PST, Amoy Yang via R-help <r-help at r-project.org> 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 MVAR itself?since MVAR is defined as "population" >with double quotes if using MVAR<-c("population") > >On Wednesday, February 3, 2016 11:54 AM, Duncan Murdoch ><murdoch.duncan at gmail.com> wrote: > > > 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) did >NOT work out. Any way that double quote can be removed as done in SAS >when creating macro_var? >> Thanks in advance for helps! > >R doesn't have a macro language, and you usually don't need one. > >If you are only reading the value of population, then > >MAVR <- population > >is fine.? This is sometimes the same as c(population), but in general >it's different:? c() will remove some attributes, such as >the dimensions on arrays. > >If you need to modify it in your program, it's likely more >complicated.? >The normal way to go would be to put your code in a function, and have >it return the modified version.? For example, > >population <- doModifications(population) > >where doModifications is a function with a definition like > >doModifications <- function(MAVR) { >? ? # do all your calculations on MAVR >? ? # then return it at the end using >? ? MAVR >} > >Duncan Murdoch > > > > [[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]]
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 not work for sqldef in R as shown below.> key<-"pop" > library(sqldf) > sqldf("select grade, count(*) as cnt, min(tab[[key]]) as min,+ max(pop) as max, avg(pop) as mean, median(pop) as median, + stdev(pop) as stdev from tab group by grade") Error in sqliteSendQuery(con, statement, bind.data) : ? error in statement: near "[[key]": syntax error On Wednesday, February 3, 2016 12:40 PM, "ruipbarradas at sapo.pt" <ruipbarradas at sapo.pt> wrote: 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 <r-help at r-project.org>: 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 "population" with double quotes if using MVAR<-c("population") ? ?On Wednesday, February 3, 2016 11:54 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote: 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) did NOT work out. Any way that double quote can be removed as done in SAS when creating macro_var? Thanks in advance for helps! R doesn't have a macro language, and you usually don't need one. If you are only reading the value of population, then MAVR <- population is fine.? This is sometimes the same as c(population), but in general it's different:? c() will remove some attributes, such as the dimensions on arrays. If you need to modify it in your program, it's likely more complicated.? The normal way to go would be to put your code in a function, and have it return the modified version.? For example, population <- doModifications(population) where doModifications is a function with a definition like doModifications <- function(MAVR) { ? ? # do all your calculations on MAVR ? ? # then return it at the end using ? ? MAVR } Duncan Murdoch ? ? ? ? [[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.htmland provide commented, minimal, self-contained, reproducible code. ? [[alternative HTML version deleted]]