Skip to content
Prev 301152 / 398506 Next

First value in a row

Hi,
I think it was due to a row with all the NAs.? I hope it happens rarely.
In those cases, you can assign NAs from looking at the list.


dat2<-data.frame(t(dat1[,3:5]))
dat3<-lapply(dat2,function(x) tail(x[!is.na(x)],1))
?dat3
$X1
[1] 0.6

$X2
[1] 0.3

$X3
[1] 0.1

$X4
numeric(0)
?dat3$X4<-NA

dat4<-data.frame(dat1,NewColumn=unlist(dat3))
rownames(dat4)<-1:nrow(dat4)
?dat4
# Lat Lon? x1? x2? x3 NewColumn
#1?? 1? 12 0.4 0.5 0.6?????? 0.6
#2?? 1? 12 0.2 0.3? NA?????? 0.3
#3?? 1? 11 0.1? NA? NA?????? 0.1
#4?? 1? 10? NA? NA? NA??????? NA
##################################
#Another way elegant 

#or you can find the NA by using

dat3<-lapply(dat2,function(x) tail(x[!is.na(x)],1))dat4<-ifelse(sapply(dat3,length)==0,NA,dat3)
dat5<-data.frame(dat1,NewColumn=unlist(dat4))
rownames(dat5)<-1:nrow(dat5)

dat5
? Lat Lon? x1? x2? x3 NewColumn
1?? 1? 12 0.4 0.5 0.6?????? 0.6
2?? 1? 12 0.2 0.3? NA?????? 0.3
3?? 1? 11 0.1? NA? NA?????? 0.1
4?? 1? 10? NA? NA? NA??????? NA


A.K.




----- Original Message -----
From: Camilo Mora <cmora at dal.ca>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Tuesday, July 24, 2012 11:30 PM
Subject: Re: First value in a row

Hi Arun,

It works partially.... your script skip those rows full of NAs causing a different vector size, which then prevents merging the results to the original database. Any way to keep rows with all NAs?

C


Camilo Mora, Ph.D.
Department of Geography, University of Hawaii
Currently available in Colombia
Phone:?  Country code: 57
? ? ? ?  Provider code: 313
? ? ? ?  Phone 776 2282
? ? ? ?  From the USA or Canada you have to dial 011 57 313 776 2282
http://www.soc.hawaii.edu/mora/



Quoting arun <smartpink111 at yahoo.com>: