basic help
Thank you, it worked perfectly. Moti -----Original Message----- From: Chuck Cleland [mailto:ccleland at optonline.net] Sent: Thursday, March 20, 2008 10:40 AM To: ???? ?????? Cc: r-help at r-project.org Subject: Re: [R] basic help
On 3/20/2008 12:59 AM, ???? ?????? wrote:
Hi,
I am a new R user (used SPSS for many years) and I need help.
I have a data frame mydata with 8 variables m2008:m2001
I wanted to add a new variable mydata$firstvalid that tells me what is the
first non missing variable for each case (without using for-next).
I tried many variations of this:
lst = paste("m",2008:2001,sep="")
mydata$firstvalid = match(FALSE, is.na(mydata[lst]),0)
Instead of a different value for each case, I get the same value for all
cases - the first non missing value in the whole data frame.
Many thanks,
Moti Assouline
X <- as.data.frame(matrix(sample(c(NA,NA,1:5), 100, replace=TRUE), ncol=5))
X
V1 V2 V3 V4 V5
1 NA NA NA NA NA
2 2 4 NA 3 3
3 2 3 NA NA NA
4 4 3 2 NA 2
5 2 3 2 NA NA
6 5 3 2 5 NA
7 NA 5 NA 3 3
8 3 NA 3 2 2
9 4 5 5 NA 3
10 2 5 NA NA 1
11 1 2 NA NA 2
12 2 4 NA 5 2
13 NA 5 NA NA NA
14 5 5 4 5 NA
15 2 NA 5 2 NA
16 NA 1 4 NA NA
17 NA 5 NA 5 NA
18 5 2 NA 4 1
19 3 5 2 4 5
20 4 NA 2 1 NA
X$FVALID <- apply(is.na(X), 1, function(x){ifelse(all(x), 0, which.min(x))})
X
V1 V2 V3 V4 V5 FVALID
1 NA NA NA NA NA 0
2 2 4 NA 3 3 1
3 2 3 NA NA NA 1
4 4 3 2 NA 2 1
5 2 3 2 NA NA 1
6 5 3 2 5 NA 1
7 NA 5 NA 3 3 2
8 3 NA 3 2 2 1
9 4 5 5 NA 3 1
10 2 5 NA NA 1 1
11 1 2 NA NA 2 1
12 2 4 NA 5 2 1
13 NA 5 NA NA NA 2
14 5 5 4 5 NA 1
15 2 NA 5 2 NA 1
16 NA 1 4 NA NA 2
17 NA 5 NA 5 NA 2
18 5 2 NA 4 1 1
19 3 5 2 4 5 1
20 4 NA 2 1 NA 1
Checked by AVG. 20:52
______________________________________________ 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.
Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 No virus found in this incoming message. Checked by AVG. 19/03/2008 09:54 Checked by AVG. 19/03/2008 09:54