Please excuse what I'm sure are very easy questions but I'm relatively new to the R environment. How can I view a range of list elements, but not all. e.g., I had a matrix of patients and then split them out by patient id. I know I can do patlist[[1]] to see the first one, but how can I view, say, the first ten patients? My other question is how to count how many patients have a record in which a certain condition holds. E.g., I was trying something like this to get a count: ctr<-0 temp<-lapply(mylist, function(x){is.na(x$date1[1]) & !is.na(x$date2[1])) ctr<-ctr+1}) But I don't think that's working correctly. Thanks, Steven
Your questions are confusing because you don't seem to understand R's basic data structures and manipulations. Please read the "Introduction to R" manual and appropriate portions of the R languuage definition before posting. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of sms13+ at pitt.edu > Sent: Friday, May 06, 2005 8:08 AM > To: r-help at stat.math.ethz.ch > Subject: [R] 2 simple questions > > Please excuse what I'm sure are very easy questions but I'm > relatively new > to the R environment. > How can I view a range of list elements, but not all. e.g., > I had a matrix > of patients and then split them out by patient id. I know I can do > patlist[[1]] to see the first one, but how can I view, say, > the first ten > patients? > > My other question is how to count how many patients have a > record in which > a certain condition holds. E.g., I was trying something like > this to get a > count: > ctr<-0 > temp<-lapply(mylist, function(x){is.na(x$date1[1]) & > !is.na(x$date2[1])) > ctr<-ctr+1}) > > But I don't think that's working correctly. > > Thanks, > Steven > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
sms13+ at pitt.edu wrote:> Please excuse what I'm sure are very easy questions but I'm relatively > new to the R environment. > How can I view a range of list elements, but not all. e.g., I had a > matrix of patients and then split them out by patient id. I know I can > do patlist[[1]] to see the first one, but how can I view, say, the first > ten patients?Use vector indexing on patlist such as patlist[1:10] which return a list of the first 10 elements. Please read the docs on object indexing.> My other question is how to count how many patients have a record in > which a certain condition holds. E.g., I was trying something like this > to get a count: > ctr<-0 > temp<-lapply(mylist, function(x){is.na(x$date1[1]) & !is.na(x$date2[1])) > ctr<-ctr+1})No, lapply is NOT a loop, instead think vectorized: ## at first look at a logical vector, where the condition holds: temp <- sapply(mylist, function(x) (is.na(x$date1[1]) & !is.na(x$date2[1]))) ctr <- sum(temp) # and sum that vector Uwe Ligges> But I don't think that's working correctly. > > Thanks, > Steven > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html