Hello! I would like to do some tables for several variables and I would like to write a loop that does the table for each variable. I should also point out that my data set has several missing observations and sometimes the observations that are missing are not the same for all my variables. What I would like to do: table(StoreData$CompanyID, !is.na(StoreData$P2)) table(StoreData$CompanyID, !is.na(StoreData$P5)) If I run the above code, I get:> table(StoreData$CompanyID,+ !is.na(StoreData$P2)) FALSE TRUE 2 940 0 3 0 323 4 288 0 5 306 0> table(StoreData$CompanyID,+ !is.na(StoreData$P5)) FALSE TRUE 2 940 0 3 0 323 4 288 0 5 306 0 Here's the loop that I wrote, which does not work: angus <- c(2,5) for(i in angus) { cut <- paste("StoreData$P",i, sep="") table(StoreData$CompanyID, !is.na(cut)) } When I run the above, I get the following error message: Error in table(StoreData$CompanyID, !is.na(cut)) : all arguments must have the same length> source(.trPaths[5], echo=TRUE, max.deparse.length=150)Any help is greatly appreciated! Stella [[alternative HTML version deleted]]
Hi Stella, in your coding 'cut' is a string, not a data object. something like cut <- paste("P",i, sep="") table(StoreData$CompanyID, !is.na(StoreData[,cut])) should work. hth. Am 09.02.2011 19:02, schrieb Rita Carreira:> > > Hello! > I would like to do some tables for several variables and I would like to write a loop that does the table for each variable. I should also point out that my data set has several missing observations and sometimes the observations that are missing are not the same for all my variables. > > What I would like to do: > > table(StoreData$CompanyID, > !is.na(StoreData$P2)) > table(StoreData$CompanyID, > !is.na(StoreData$P5)) > > If I run the above code, I get: > >> table(StoreData$CompanyID, > + !is.na(StoreData$P2)) > > FALSE TRUE > 2 940 0 > 3 0 323 > 4 288 0 > 5 306 0 > >> table(StoreData$CompanyID, > + !is.na(StoreData$P5)) > > FALSE TRUE > 2 940 0 > 3 0 323 > 4 288 0 > 5 306 0 > > > Here's the loop that I wrote, which does not work: > > angus <- c(2,5) > > for(i in angus) { > cut <- paste("StoreData$P",i, sep="") > table(StoreData$CompanyID, !is.na(cut)) > } > > When I run the above, I get the following error message: > > Error in table(StoreData$CompanyID, !is.na(cut)) : > all arguments must have the same length >> source(.trPaths[5], echo=TRUE, max.deparse.length=150) > > Any help is greatly appreciated! > Stella > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
Try: for(i in angus) { cut <- StoreData[[paste("P", i)]] table(StoreData$CompanyID, !is.na(cut)) } On Wed, Feb 9, 2011 at 1:02 PM, Rita Carreira <ritacarreira at hotmail.com> wrote:> > > Hello! > I would like to do some tables for several variables and I would like to write a loop that does the table for each variable. I should also point out that my data set has several missing observations and sometimes the observations that are missing are not the same for all my variables. > > What I would like to do: > > table(StoreData$CompanyID, > ? ? ? ?!is.na(StoreData$P2)) > table(StoreData$CompanyID, > ? ? ? ?!is.na(StoreData$P5)) > > If I run the above code, I get: > >> table(StoreData$CompanyID, > + ? ? ? ? !is.na(StoreData$P2)) > > ? ?FALSE TRUE > ?2 ? 940 ? ?0 > ?3 ? ? 0 ?323 > ?4 ? 288 ? ?0 > ?5 ? 306 ? ?0 > >> table(StoreData$CompanyID, > + ? ? ? ? !is.na(StoreData$P5)) > > ? ?FALSE TRUE > ?2 ? 940 ? ?0 > ?3 ? ? 0 ?323 > ?4 ? 288 ? ?0 > ?5 ? 306 ? ?0 > > > Here's the loop that I wrote, which does not work: > > angus <- c(2,5) > > for(i in angus) { > ?cut <- paste("StoreData$P",i, sep="") > ?table(StoreData$CompanyID, !is.na(cut)) > ?} > > When I run the above, I get the following error message: > > Error in table(StoreData$CompanyID, !is.na(cut)) : > ?all arguments must have the same length >> source(.trPaths[5], echo=TRUE, max.deparse.length=150) > > Any help is greatly appreciated! > Stella > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Hi Stella, if you just want to print the tables, this should also work for(i in angus) { tab <- paste("table", i, sep="") cut <- paste("P",i, sep = "") print(table(StoreData$CompanyID, !is.na(StoreData[,cut]))) } If you want to keep them, your approach works, but you can also store them in a list tabs<-list() for(i in angus) { tab <- paste("table", i, sep="") cut <- paste("P",i, sep = "") tabs[[i]]<-table(StoreData$CompanyID, !is.na(StoreData[,cut])) } this produces a list of 5 elements, where only 2 and 5 are populated. Changing the last line to tabs[[which(angus==i)]]<-table(StoreData$CompanyID, !is.na(StoreData[,cut])) produces a list of 2 elements, but the information on "angus" is lost. best regards Am 11.02.2011 15:41, schrieb Rita Carreira:> Thanks so much, that worked well; however, it did not print the tables. > I went around it and did the following, which worked: > > for(i in angus) { > tab <- paste("table", i, sep="") > cut <- paste("P",i, sep = "") > t <- table(StoreData$CompanyID, !is.na(StoreData[,cut])) > assign(tab,t) > } > > table2 > table5 > > Is this the only way? Could I not have put table2 and table5 with an i > index inside the loop? > > Sorry to bother you. > Stella > > >> Date: Wed, 9 Feb 2011 20:15:23 +0100 >> From: E.Vettorazzi at uke.uni-hamburg.de >> To: ritacarreira at hotmail.com >> CC: r-help at r-project.org >> Subject: Re: [R] Loop in variable names >> >> Hi Stella, >> in your coding 'cut' is a string, not a data object. >> >> something like >> cut <- paste("P",i, sep="") >> table(StoreData$CompanyID, !is.na(StoreData[,cut])) >> >> should work. >> >> hth. >> >> Am 09.02.2011 19:02, schrieb Rita Carreira: >> > >> > >> > Hello! >> > I would like to do some tables for several variables and I would > like to write a loop that does the table for each variable. I should > also point out that my data set has several missing observations and > sometimes the observations that are missing are not the same for all my > variables. >> > >> > What I would like to do: >> > >> > table(StoreData$CompanyID, >> > !is.na(StoreData$P2)) >> > table(StoreData$CompanyID, >> > !is.na(StoreData$P5)) >> > >> > If I run the above code, I get: >> > >> >> table(StoreData$CompanyID, >> > + !is.na(StoreData$P2)) >> > >> > FALSE TRUE >> > 2 940 0 >> > 3 0 323 >> > 4 288 0 >> > 5 306 0 >> > >> >> table(StoreData$CompanyID, >> > + !is.na(StoreData$P5)) >> > >> > FALSE TRUE >> > 2 940 0 >> > 3 0 323 >> > 4 288 0 >> > 5 306 0 >> > >> > >> > Here's the loop that I wrote, which does not work: >> > >> > angus <- c(2,5) >> > >> > for(i in angus) { >> > cut <- paste("StoreData$P",i, sep="") >> > table(StoreData$CompanyID, !is.na(cut)) >> > } >> > >> > When I run the above, I get the following error message: >> > >> > Error in table(StoreData$CompanyID, !is.na(cut)) : >> > all arguments must have the same length >> >> source(.trPaths[5], echo=TRUE, max.deparse.length=150) >> > >> > Any help is greatly appreciated! >> > Stella >> > >> > >> > >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > R-help at 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. >> >> >> -- >> Eik Vettorazzi >> Institut f?r Medizinische Biometrie und Epidemiologie >> Universit?tsklinikum Hamburg-Eppendorf >> >> Martinistr. 52 >> 20246 Hamburg >> >> T ++49/40/7410-58243 >> F ++49/40/7410-57790-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
Seemingly Similar Threads
- R not working after loading lattice
- Function for deleting variables with >=50% missing obs from a data frame
- How do I delete multiple blank variables from a data frame?
- looping using 'diverse' package measures
- What does class "call" mean? How do I make class "formula" into a "call"?