[R] First value in a row

arun smartpink111 at yahoo.com
Wed Jul 25 05:50:45 CEST 2012


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>:

> 
> 
> Hi Camilo,
> 
> You can either use Henrik's or mine to find it,
>  unlist(apply(dat1[,-(1:2)],1,function(x) tail(x[!is.na(x)],1)))
>  x3  x2  x1
> 0.6 0.3 0.1
> 
> #or you can use my functiton
> 
> dat3<-data.frame(NewColumn=c(unlist(lapply(dat2,function(x) tail(x[!is.na(x)],1))),NA))
>  dat4<-data.frame(dat1,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
> 
> A.K.
> 
> 
> 
> 
> 
> 
> ----- Original Message -----
> From: Camilo Mora <cmora at dal.ca>
> To: arun <smartpink111 at yahoo.com>
> Cc: Henrik Singmann <henrik.singmann at psychologie.uni-freiburg.de>; R help <r-help at r-project.org>
> Sent: Tuesday, July 24, 2012 10:56 PM
> Subject: Re: First value in a row
> 
> Hi Henrik and Arun,
> 
> I now understand the script you provided. Very smart solution I think. I wonder, however, if there is an alternative way as to count the last number in a row?.
> For instance, considering the following dataframe
> 
> dat1<-read.table(text="
> Lat  Lon  x1  x2  x3
> 01    12  .4  .5  .6
> 01    12  .2  .3  NA
> 01    11  .1  NA  NA
> 01    10  NA  NA  NA
> ",sep="",header=TRUE)
> 
> the last value (from left to right) should be:
> .6
> .3
> .1
> NA
> 
> NAs are always consecutive once they appear.
> 
> Thanks again,
> 
> Camilo
> 
> 
> 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>:
> 
>> Hi Henrik,
>> 
>> Thanks for testing it to a different dataset.  I didn't test it at that time to multiple conditions.  Probably, apply is a better method.
>> 
>> 
>> Anyway, you can still get the same result by doing this:
>> 
>> dat1<-read.table(text="
>> Lat  Lon  x1  x2  x3
>> 01    10  NA  NA  .1
>> 01    11  .4  NA  .3
>> 01    12  NA  .5  .6
>> ",sep="",header=TRUE)
>> dat2<-data.frame(t(dat1[,3:5]))
>> dat3<-data.frame(dat1,NewColumn=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 0.4  NA 0.3       0.4
>> #3   1  12  NA 0.5 0.6       0.5
>> 
>> #Now, to a slightly different dataset
>> dat1<-read.table(text="
>> Lat  Lon  x1  x2  x3
>> 01    10  NA  NA  NA
>> 01    11  NA  NA  .3
>> 01    12  NA  .6   NA
>> ",sep="",header=TRUE)
>>  dat2<-data.frame(t(dat1[,3:5]))
>>  dat3<-data.frame(dat1,NewColumn=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  NA        NA
>> #2   1  11 NA  NA 0.3       0.3
>> #3   1  12 NA 0.6  NA       0.6
>> 
>> 
>> I hope this works well.
>> 
>> 
>> A.K.
>> 
>> 
>> 
>> 
>> ----- Original Message -----
>> From: Henrik Singmann <henrik.singmann at psychologie.uni-freiburg.de>
>> To: arun <smartpink111 at yahoo.com>
>> Cc: Camilo Mora <cmora at dal.ca>; R help <r-help at r-project.org>
>> Sent: Tuesday, July 24, 2012 10:18 AM
>> Subject: Re: First value in a row
>> 
>> Hi,
>> 
>> As Arun's idea was also my first idea let me pinpoint the problem of this solution.
>> It only works if the data in question (i.e., columns x1 to x3) follow the pattern of the example data insofar that the NAs form a triangle like structure. This is so because it loops over columns instead of rows and takes advantage of the triangle NA structure.
>> 
>> For example, slightly changing the data leads to a result that does not follow the description of Camilo seem to want:
>> 
>> dat1<-read.table(text="
>> Lat  Lon  x1  x2  x3
>> 01    10  NA  NA  .1
>> 01    11  .4  NA  .3
>> 01    12  NA  .5  .6
>> ",sep="",header=TRUE)
>> 
>> # correct answer from description would be .1, .4, .5
>> 
>> # arun's solution:
>> data.frame(dat1,NewColumn=rev(unlist(lapply(dat1[,3:5],function(x) x[!is.na(x)][1]))))
>> 
>> #  x3  x2  x1
>> # 0.1 0.5 0.4
>> 
>> # my solution:
>> apply(dat1[,-(1:2)], 1, function(x) x[!is.na(x)][1])
>> 
>> # [1] 0.1 0.4 0.5
>> 
>> So the question is, what you want and how the data looks.
>> 
>> Cheers,
>> Henrik
>> 
>> 
>> Am 24.07.2012 14:27, schrieb arun:
>>> 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.
>>> 
>>> 
>> 
>> --
>> Dipl. Psych. Henrik Singmann
>> PhD Student
>> Albert-Ludwigs-Universität Freiburg, Germany
>> http://www.psychologie.uni-freiburg.de/Members/singmann
>> 
>> 
>> 
> 
>


More information about the R-help mailing list