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]]
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,") as min, max(",key,") as max, avg(",key,") as mean, median(",key,") as median, stdev(",key,") as stdev from tab group by grade" ) Then print(sql.stmt, quote=FALSE) to make sure I got what I wanted. Use paste0() instead of paste() if you don't want the extra space characters, even though they don't matter to SQL. And, no, the double [[...]] does not remove double quotes. That's not how it works. tab[['pop']] tab[ , 'pop'] tab$pop are three different ways to reference the pop column in the tab data frame. Of the three, the value 'pop' can be stored in a variable with the first two, but not the third one. Remember, R is not SAS, so you have to use a different technique to construct the desired SQL statement; there is nothing in R that works exactly like SAS macro variables. (R has other advantages). -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 2/3/16, 11:16 AM, "R-help on behalf of Amoy Yang via R-help" <r-help-bounces at r-project.org on behalf of r-help at r-project.org> wrote:>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]] > >______________________________________________ >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.
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 <r-help at r-project.org> wrote: 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.htmlandprovide commented, minimal, self-contained, reproducible code. ? ? ??? [[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]]
> On Feb 3, 2016, at 11:57 AM, Amoy Yang via R-help <r-help at r-project.org> 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( paste("select", MVAR, " from tab") ) -- David.> > On Wednesday, February 3, 2016 1:18 PM, Amoy Yang via R-help <r-help at r-project.org> wrote: > > > 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.htmlandprovide commented, minimal, self-contained, reproducible code. > > > > > [[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]] > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA
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 <r-help at r-project.org> wrote:> 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]] > > ______________________________________________ > 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.-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
One more question (see below). I cannot use macro-var, mvar, for creating new name, as shown below. Any?advice is highly appreciated!> mvar<-"pop" > new.pop<-tab[[mvar]]; new.pop?[1]? 698 1214 1003 1167 2549? 824? 944 1937? 935? 570??? 0> new.tab[[mvar]]<-d$pop;Error in new.tab[[mvar]] <- d$pop : object 'new.tab' not found On Thursday, February 4, 2016 11:02 AM, Amoy Yang <amoy_y at yahoo.com> wrote: This works although it looks rare by using min(",key,"). Don't know why but just have to remember it. This is a tough part in R. Thanks for helps! Amoy On Wednesday, February 3, 2016 5:25 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: 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 <r-help at r-project.org> wrote:> 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.htmlandprovide commented, minimal, self-contained, reproducible code. > > > > >? ? ? ? [[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.-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com [[alternative HTML version deleted]]
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 > dfa b 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6> mvar <- 'c' > df[[mvar]] <- 0:4 > dfa b c 1 1 2 0 2 2 3 1 3 3 4 2 4 4 5 3 5 5 6 4 In your case, the object named "new.tab" does not exist when you try to create a new variable in it. That's what the error message says. Try, perhaps, new.tab <- tab new.tab[[mvar]] <- d$pop (and hope that the number of elements in d$pop is the same as the number of rows in new.tab) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 2/5/16, 8:53 AM, "R-help on behalf of Amoy Yang via R-help" <r-help-bounces at r-project.org on behalf of r-help at r-project.org> wrote:> > > One more question (see below). I cannot use macro-var, mvar, for >creating new name, as shown below. Any advice is highly appreciated! > >> mvar<-"pop" >> new.pop<-tab[[mvar]]; new.pop > [1] 698 1214 1003 1167 2549 824 944 1937 935 570 0 >> new.tab[[mvar]]<-d$pop; >Error in new.tab[[mvar]] <- d$pop : object 'new.tab' not found > > On Thursday, February 4, 2016 11:02 AM, Amoy Yang <amoy_y at yahoo.com> >wrote: > > > This works although it looks rare by using min(",key,"). Don't know why >but just have to remember it. This is a tough part in R. >Thanks for helps! >Amoy > > On Wednesday, February 3, 2016 5:25 PM, Gabor Grothendieck ><ggrothendieck at gmail.com> wrote: > > > 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 ><r-help at r-project.org> wrote: >> 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.htmlandprovide commented, >>minimal, self-contained, reproducible code. >> >> >> >> >> [[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. > > > >-- >Statistics & Software Consulting >GKX Group, GKX Associates Inc. >tel: 1-877-GKX-GROUP >email: ggrothendieck 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.
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]] <- newColumn If you have a fixed name for the new column (not a variable containing the name), you can also use new.tab <- cbind(new.tab, newName=newColumn) new.tab$newName <- newColumn Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Feb 5, 2016 at 8:53 AM, Amoy Yang via R-help <r-help at r-project.org> wrote:> > > One more question (see below). I cannot use macro-var, mvar, for creating > new name, as shown below. Any advice is highly appreciated! > > > mvar<-"pop" > > new.pop<-tab[[mvar]]; new.pop > [1] 698 1214 1003 1167 2549 824 944 1937 935 570 0 > > new.tab[[mvar]]<-d$pop; > Error in new.tab[[mvar]] <- d$pop : object 'new.tab' not found > > On Thursday, February 4, 2016 11:02 AM, Amoy Yang <amoy_y at yahoo.com> > wrote: > > > This works although it looks rare by using min(",key,"). Don't know why > but just have to remember it. This is a tough part in R. > Thanks for helps! > Amoy > > On Wednesday, February 3, 2016 5:25 PM, Gabor Grothendieck < > ggrothendieck at gmail.com> wrote: > > > 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 > <r-help at r-project.org> wrote: > > 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.htmlandprovide commented, minimal, > self-contained, reproducible code. > > > > > > > > > > [[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. > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck 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]]
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 an element of a list. A data.frame is a list of vectors (all of the same length). A vector of mode "numeric" is just numbers, not a list. You created a new variable new.pop that holds a numeric vector. You printed it and confirmed that that is what it is. You then tried to refer to a variable that you have NOT created, new.tab. However, if you had tried the expression new.pop[[mvar]] you would have been trying to treat a numeric vector as a list, which it is not... and if it was it would have to have an element named pop inside it already to extract something, which it doesn't. A key step in getting out of your state of confusion is to learn how objects can contain other objects, and when to work with containing objects and when to work work contained objects. Some possible solutions: new.tab <- data.frame( pop=new.pop ) or new.tab <- data.frame( pop = tab[[mvar]] ) or new.tab <- tab[ , "pop", drop=FALSE ) With which you can then add new columns new.tab$pop2 <- new.pop ^2 new.pop[[ "pop3" ]] <- new.pop^3 -- Sent from my phone. Please excuse my brevity. On February 5, 2016 8:53:28 AM PST, Amoy Yang via R-help <r-help at r-project.org> wrote:> > >One more question (see below). I cannot use macro-var, mvar, for >creating new name, as shown below. Any?advice is highly appreciated! > >> mvar<-"pop" >> new.pop<-tab[[mvar]]; new.pop >?[1]? 698 1214 1003 1167 2549? 824? 944 1937? 935? 570??? 0 >> new.tab[[mvar]]<-d$pop; >Error in new.tab[[mvar]] <- d$pop : object 'new.tab' not found > >On Thursday, February 4, 2016 11:02 AM, Amoy Yang <amoy_y at yahoo.com> >wrote: > > >This works although it looks rare by using min(",key,"). Don't know why >but just have to remember it. This is a tough part in R. >Thanks for helps! >Amoy > >On Wednesday, February 3, 2016 5:25 PM, Gabor Grothendieck ><ggrothendieck at gmail.com> wrote: > > > 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 ><r-help at r-project.org> wrote: >> 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.htmlandprovide commented, >minimal, self-contained, reproducible code. >> >> >> >> >>? ? ? ? [[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. > > > >-- >Statistics & Software Consulting >GKX Group, GKX Associates Inc. >tel: 1-877-GKX-GROUP >email: ggrothendieck 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]]