Re: [R] How to translate string to variable inside a command in an easy way in R
Thanks Here is another question I want to have a function that get a string for example y="AMI" and make commands like the following agg<-aggregate(numAMI ~MemberID,right.a,sum) Note that I know that numAMI is part of right.a because another function with the string AMI already generated it. Is there a way to do it without paste parse and eval? I can do it by the following commands for y="AMI" numy<-paste("num",y,sep="") texta<-paste("agg<-aggregate(",numy,sep="") text2<-"~MemberID, right.a, sum)" text1<-paste(texta,text2,sep="") eval(parse(text=text1)) If you can have a shorter code for it then it can be productive. -- View this message in context: http://r.789695.n4.nabble.com/How-to-translate-string-to-variable-inside-a-command-in-an-easy-way-in-R-tp3645594p3665462.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] How to translate string to variable inside a command in an easy way in R
Greg Snow-2 wrote: "You are suffering from the fact that the longest distance between 2 points is a shortcut. The df$column notation is a shortcut for df[[column]] that has some nice properties, but the shortcut gets in the way when you want to do something more structured. Try qq1[[z]]==y and avoid all that pasting, parsing, and evaluating. " My response: Unfortunately I find it to be not correct and unfortunately I see no way to avoid pasting parsing and evaluating in order to have functions that does simple things in R. temp<-qq1[qq1$PrimaryConditionGroup=="AMI",] has no error when temp<-qq1[qq1[[PrimaryConditionGroup]]=="AMI",] cause the following error announcement "Error in (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, : object 'PrimaryConditionGroup' not found" I found no way to replace the function that was in the subject of this thread by something simpler(and here is the function again) > ugly<-function(y,z) > { > text1<-paste("temp<-qq1[qq1$",z,sep="") > text1<-paste(text1,"==y",sep="") > text1<-paste(text1,",]",sep="") > eval(parse(text=text1)) > temp<<-temp > } -- View this message in context: http://r.789695.n4.nabble.com/How-to-translate-string-to-variable-inside-a-command-in-an-easy-way-in-R-tp3645594p3664747.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
[R] How to translate string to variable inside a command in an easy way in R
I want to write a function that get 2 strings y and z and does the following R command. temp<-qq1[qq1$z==y,] for example if it get y="AMI" and z="PrimaryConditionGroup" It should do the following temp<-qq1[qq1$PrimaryConditionGroup=="AMI",] I could do it by the following function that is ugly and I wonder if there is an easier way to do it espacielly when temp is not the final result that I want (so I practically do not have temp<<-temp because I do not need the function to remember temp but only to remember something else that is calculated based on temp). ugly<-function(y,z) { text1<-paste("temp<-qq1[qq1$",z,sep="") text1<-paste(text1,"==y",sep="") text1<-paste(text1,",]",sep="") eval(parse(text=text1)) temp<<-temp } -- View this message in context: http://r.789695.n4.nabble.com/How-to-translate-string-to-variable-inside-a-command-in-an-easy-way-in-R-tp3645594p3645594.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] How to build a matrix of number of appearance?
Thanks for your reply Note that I guess that there are many providerID and I get the error cannot allocate vector of size 2.1 Gb (I can use the same trick for most of the other fields) Is there a way to do the same only for providerID with relatively high frequency? -- View this message in context: http://r.789695.n4.nabble.com/How-to-build-a-matrix-of-number-of-appearance-tp3643248p3645550.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
[R] How to build a matrix of number of appearance?
I have a matrix of claims at year1 that I get simply by claims<-read.csv(file="Claims.csv") qq1<-claims[claims$Year=="Y1",] I have MemberID and ProviderID for every claim in qq1 both are integers An example for the type of questions that I want to answer is how many times ProviderID number 345 appears together with MemberID 23 in the table qq1 In order to answer these questions for every possible ProviderId and every possible MemberID I would like to have a matrix that has first column as memberID when every memberID in qq1 appears only once and columns that have number of appearance of ProviderID==i for every i that has sum(qq1$ProviderID==i)>0 My question is if there is a simple way to do it in R Thanks in Advance Uri -- View this message in context: http://r.789695.n4.nabble.com/How-to-build-a-matrix-of-number-of-appearance-tp3643248p3643248.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.