najuzz
2014-Dec-22 13:45 UTC
[R] number of individuals where X=0 during all periods (longitudinal data)
#Hi guys, #I would like to count the number of individuals that receive X=0 troughout their observational period. #example dataset: ID<-c(1,1,1,1,2,2,3,3,3) X<-c(0,1,2,1,0,0,0,0,0) Time<-c(1,2,3,4,1,2,1,2,3) Test<-data.frame(ID,X,Time) # Individuals 2 and 3 have x=0 during all their periods. The count should hence equal to two. I simply have # no clue how R could solve this for me. As an addon, I would also like to know the number of individuals #that report X=0 during all periods plus have at least 3 weeks of observations. The answer would be one in #this sample datset. #Thank you -- View this message in context: http://r.789695.n4.nabble.com/number-of-individuals-where-X-0-during-all-periods-longitudinal-data-tp4701023.html Sent from the R help mailing list archive at Nabble.com.
David L Carlson
2014-Dec-22 16:46 UTC
[R] number of individuals where X=0 during all periods (longitudinal data)
Spend a little time with aggregate() ?aggregate ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of najuzz Sent: Monday, December 22, 2014 7:45 AM To: r-help at r-project.org Subject: [R] number of individuals where X=0 during all periods (longitudinal data) #Hi guys, #I would like to count the number of individuals that receive X=0 troughout their observational period. #example dataset: ID<-c(1,1,1,1,2,2,3,3,3) X<-c(0,1,2,1,0,0,0,0,0) Time<-c(1,2,3,4,1,2,1,2,3) Test<-data.frame(ID,X,Time) # Individuals 2 and 3 have x=0 during all their periods. The count should hence equal to two. I simply have # no clue how R could solve this for me. As an addon, I would also like to know the number of individuals #that report X=0 during all periods plus have at least 3 weeks of observations. The answer would be one in #this sample datset. #Thank you -- View this message in context: http://r.789695.n4.nabble.com/number-of-individuals-where-X-0-during-all-periods-longitudinal-data-tp4701023.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Chel Hee Lee
2014-Dec-22 18:54 UTC
[R] number of individuals where X=0 during all periods (longitudinal data)
> tmp <- split(Test, Test$ID)> > # number of subjects with X=0 (all periods) > x <- lapply(tmp, function(x) all(x$X ==0)) > length(x[unlist(x)]) [1] 2 > > # number of subjects with X=0 (at least three weeks) > x <- lapply(tmp, function(x) sum(x$X==0)>=3) > length(x[unlist(x)]) [1] 1 > Is this what you are looking for? I hope this helps. Chel Hee Lee On 12/22/2014 7:45 AM, najuzz wrote:> #Hi guys, > > #I would like to count the number of individuals that receive X=0 troughout > their observational period. > #example dataset: > > ID<-c(1,1,1,1,2,2,3,3,3) > X<-c(0,1,2,1,0,0,0,0,0) > Time<-c(1,2,3,4,1,2,1,2,3) > Test<-data.frame(ID,X,Time) > > # Individuals 2 and 3 have x=0 during all their periods. The count should > hence equal to two. I simply have > # no clue how R could solve this for me. As an addon, I would also like to > know the number of individuals #that report X=0 during all periods plus > have at least 3 weeks of observations. The answer would be one in #this > sample datset. > > #Thank you > > > > -- > View this message in context: http://r.789695.n4.nabble.com/number-of-individuals-where-X-0-during-all-periods-longitudinal-data-tp4701023.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
William Dunlap
2014-Dec-22 19:52 UTC
[R] number of individuals where X=0 during all periods (longitudinal data)
Another approach to to make a table and extract your summaries from the table: > tbl <- with(Test, table(ID, IsZero=X==0)) > tbl IsZero ID FALSE TRUE 1 3 1 2 0 2 3 0 3 > sum(tbl[,"FALSE"] == 0) [1] 2 > sum(tbl[,"FALSE"] == 0 & rowSums(tbl)>=3) [1] 1 Remove the calls to sum() from those expressions and you will see which ID's satisfy the conditions. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 22, 2014 at 10:54 AM, Chel Hee Lee <chl948 at mail.usask.ca> wrote:> > > tmp <- split(Test, Test$ID) > > > > # number of subjects with X=0 (all periods) > > x <- lapply(tmp, function(x) all(x$X ==0)) > > length(x[unlist(x)]) > [1] 2 > > > > # number of subjects with X=0 (at least three weeks) > > x <- lapply(tmp, function(x) sum(x$X==0)>=3) > > length(x[unlist(x)]) > [1] 1 > > > > Is this what you are looking for? I hope this helps. > > Chel Hee Lee > > On 12/22/2014 7:45 AM, najuzz wrote: > >> #Hi guys, >> >> #I would like to count the number of individuals that receive X=0 >> troughout >> their observational period. >> #example dataset: >> >> ID<-c(1,1,1,1,2,2,3,3,3) >> X<-c(0,1,2,1,0,0,0,0,0) >> Time<-c(1,2,3,4,1,2,1,2,3) >> Test<-data.frame(ID,X,Time) >> >> # Individuals 2 and 3 have x=0 during all their periods. The count should >> hence equal to two. I simply have >> # no clue how R could solve this for me. As an addon, I would also like to >> know the number of individuals #that report X=0 during all periods plus >> have at least 3 weeks of observations. The answer would be one in #this >> sample datset. >> >> #Thank you >> >> >> >> -- >> View this message in context: http://r.789695.n4.nabble.com/ >> number-of-individuals-where-X-0-during-all-periods- >> longitudinal-data-tp4701023.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. >> >> > ______________________________________________ > 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]]
Barry Rowlingson
2014-Dec-23 08:24 UTC
[R] number of individuals where X=0 during all periods (longitudinal data)
obligatory dplyr solution: library(dplyr) zeroes = Test %>% group_by(ID) %>% summarise(Zs=all(X==0)) - that gives you a data frame of unique ID and Zs==TRUE if that ID has all zeroes. With that you just sum(zeroes$Zs) to get the total number with all zeroes. Or add "%>% filter(Zs) %>% nrow()" to the pipeline to get it directly. Its well worth learning a bit about the dplyr and tidyr package, especially if you have a lot of things you might want to do by ID in a data frame. Barry On Mon, Dec 22, 2014 at 7:52 PM, William Dunlap <wdunlap at tibco.com> wrote:> Another approach to to make a table and extract your summaries from the > table: > > > tbl <- with(Test, table(ID, IsZero=X==0)) > > tbl > IsZero > ID FALSE TRUE > 1 3 1 > 2 0 2 > 3 0 3 > > sum(tbl[,"FALSE"] == 0) > [1] 2 > > sum(tbl[,"FALSE"] == 0 & rowSums(tbl)>=3) > [1] 1 > > Remove the calls to sum() from those expressions and you will see which > ID's satisfy the conditions. > > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Mon, Dec 22, 2014 at 10:54 AM, Chel Hee Lee <chl948 at mail.usask.ca> wrote: >> >> > tmp <- split(Test, Test$ID) >> > >> > # number of subjects with X=0 (all periods) >> > x <- lapply(tmp, function(x) all(x$X ==0)) >> > length(x[unlist(x)]) >> [1] 2 >> > >> > # number of subjects with X=0 (at least three weeks) >> > x <- lapply(tmp, function(x) sum(x$X==0)>=3) >> > length(x[unlist(x)]) >> [1] 1 >> > >> >> Is this what you are looking for? I hope this helps. >> >> Chel Hee Lee >> >> On 12/22/2014 7:45 AM, najuzz wrote: >> >>> #Hi guys, >>> >>> #I would like to count the number of individuals that receive X=0 >>> troughout >>> their observational period. >>> #example dataset: >>> >>> ID<-c(1,1,1,1,2,2,3,3,3) >>> X<-c(0,1,2,1,0,0,0,0,0) >>> Time<-c(1,2,3,4,1,2,1,2,3) >>> Test<-data.frame(ID,X,Time) >>> >>> # Individuals 2 and 3 have x=0 during all their periods. The count should >>> hence equal to two. I simply have >>> # no clue how R could solve this for me. As an addon, I would also like to >>> know the number of individuals #that report X=0 during all periods plus >>> have at least 3 weeks of observations. The answer would be one in #this >>> sample datset. >>> >>> #Thank you >>> >>> >>> >>> -- >>> View this message in context: http://r.789695.n4.nabble.com/ >>> number-of-individuals-where-X-0-during-all-periods- >>> longitudinal-data-tp4701023.html >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> ______________________________________________ >>> 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. >>> >>> >> ______________________________________________ >> 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.