An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110209/440bc247/attachment.pl>
Loop in variable names
3 messages · Rita Carreira, Eik Vettorazzi, jim holtman
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?