low 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)
>>&
.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)
uot;",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.
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]))
e
> Subject: Re: [R] 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 followi
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
To: r-help@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 h
n(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 -
&
unlap tibco.com
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
> Behalf Of Henrik Singmann
> Sent: Tuesday, July 24, 2012 1:40 AM
> To: r-h...@stat.math.ethz.ch
> Subject: Re: [R] First value in a row
>
> Hi Camilo,
>
>
- Original Message -
From: Camilo Mora
To: r-help@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
0110 NA NA .1
lt;-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
To: r-help@r-project.org
Cc:
Sent: Tuesday, July 24, 2012 2:48 AM
Subject: [R] First value
Hi Camilo,
as you want to work on all rows, apply() is your friend.
In the following, I use an anonymous function getting the first non-na
value while looping over each row:
dat <- read.table(text = "
Lat Lon x1 x2 x3
0110 NA NA .1
0111 NA .2 .3
0112 .4 .5 .6
"
Hi.
This is likely a trivial problem but have not found a solution.
Imagine the following dataframe:
Lat Lon x1 x2 x3
0110 NA NA .1
0111 NA .2 .3
0112 .4 .5 .6
I want to generate another column that consist of the first value in
each row from columns x1 to
12 matches
Mail list logo