[R] First value in a row
arun
smartpink111 at yahoo.com
Tue Jul 24 14:27:09 CEST 2012
Hi,
Try this:
dat1<-read.table(text="
Lat Lon x1 x2 x3
01 10 NA NA .1
01 11 NA .2 .3
01 12 .4 .5 .6
",sep="",header=TRUE)
dat2<-dat1[,3:5]
dat3<-data.frame(dat1,NewColumn=rev(unlist(lapply(dat2,function(x) x[!is.na(x)][1]))))
row.names(dat3)<-1:nrow(dat3)
dat3
Lat Lon x1 x2 x3 NewColumn
1 1 10 NA NA 0.1 0.1
2 1 11 NA 0.2 0.3 0.2
3 1 12 0.4 0.5 0.6 0.4
A.K.
----- Original Message -----
From: Camilo Mora <cmora at dal.ca>
To: r-help at r-project.org
Cc:
Sent: Tuesday, July 24, 2012 2:48 AM
Subject: [R] First value in a row
Hi.
This is likely a trivial problem but have not found a solution. Imagine the following dataframe:
Lat Lon x1 x2 x3
01 10 NA NA .1
01 11 NA .2 .3
01 12 .4 .5 .6
I want to generate another column that consist of the first value in each row from columns x1 to x3. That is
NewColumn
.1
.2
.4
Any input greatly appreciated,
Thanks,
Camilo
Camilo Mora, Ph.D.
Department of Geography, University of Hawaii
______________________________________________
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.
More information about the R-help
mailing list