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))
Hi Camilo,
Forgot dat2:
#same as in previous reply.
dat2-data.frame(t(dat1[,3:5]))
A.K.
- Original Message -
From: Camilo Mora cm...@dal.ca
To: arun smartpink...@yahoo.com
Cc: Henrik Singmann henrik.singm...@psychologie.uni-freiburg.de; R help
r-help@r-project.org
Sent: Tuesday,
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
Hi Camilo,
Glad it worked well.
You mentioned replacing zeros with 1s. You can use the same function, replace
NA by 1.
dat4-ifelse(sapply(dat3,length)==0,1,dat3)
A.K.
- Original Message -
From: Camilo Mora cm...@dal.ca
To: arun smartpink...@yahoo.com
Cc:
Sent: Wednesday,
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,
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
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
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,
as you want to work on all rows, apply() is your friend
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
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
0112 .4 .5
This would work:
X - lapply(1:nrow(dat1), function(i) rev(dat1[i, -c(1:2)]))sapply(X,
function(x) x[!is.na(x)][1])
Date: Tue, 24 Jul 2012 23:56:17 -0300
From: cm...@dal.ca
To: smartpink...@yahoo.com
CC: r-help@r-project.org; henrik.singm...@psychologie.uni-freiburg.de
Subject: Re: [R
Try this:
which(diff(is.na(inc)) 0)
On Tue, Jun 23, 2009 at 11:00 AM, Alfredo Alessandrini alfreal...@gmail.com
wrote:
Hi,
I've a vector like this:
inc
[1]NANANANANANANA
[8]NANANANANA
Now is ok...thanks.
Alfredo
2009/6/23 Alfredo Alessandrini alfreal...@gmail.com:
I've the NA value also between the value of the vector:
inc
[1] NA NA NA NA NA NA NA
[8] NA NA NA NA NA NA NA
On Jun 23, 2009, at 10:00 AM, Alfredo Alessandrini wrote:
Hi,
I've a vector like this:
inc
[1]NANANANANA
NANA
[8]NANANANANA
NANA
[15]NANANANA
I've the NA value also between the value of the vector:
inc
[1]NANANANANANANA
[8]NANANANANANANA
[15]NANANANANANANA
[22]
Alfredo Alessandrini wrote:
Hi,
I've a vector like this:
inc
[1]NANANANANANANA
[8]NANANANANANANA
[15]NANANANANANA
On Tue, Jun 23, 2009 at 04:00:21PM +0200, Alfredo Alessandrini wrote:
I've a vector like this:
inc
[1]NANANANANANANA
[8]NANANANANANANA
[15]NANANA
min(which(is.na(inc)==FALSE)) #index
inc[min(which(is.na(inc)==FALSE))] #value
hth
daniel
-
cuncta stricte discussurus
-
-Ursprüngliche Nachricht-
Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im
Auftrag von
You can use this:
inc[which(!is.na(inc))[1]]
On Tue, Jun 23, 2009 at 11:08 AM, Alfredo Alessandrini alfreal...@gmail.com
wrote:
I've the NA value also between the value of the vector:
inc
[1]NANANANANANANA
[8]NANA
I think what you mean is that you want to find the position of the first
non-NA value in the vector. is.na returns a boolean vector of the NA
values, so:
xx - c(NA,NA,NA,2,3,NA,4)
which(!is.na(xx))[1]
[1] 4
The other proposed solution,
which(diff(is.na(inc)) 0)
is incorrect:
Hi
r-help-boun...@r-project.org napsal dne 23.06.2009 16:04:49:
Try this:
which(diff(is.na(inc)) 0)
Shall it be
which(diff(is.na(inc)) 0)+1
or
sum(is.na(inc))+1
or any other suitable construct with is.na
Regards
Petr
On Tue, Jun 23, 2009 at 11:00 AM, Alfredo Alessandrini
The various which(is.na) sort of things are fine, but this problem is
just screaming to be solved with rle().
Just do rle(is.na(inc))$lengths[1]+1
On Tue, Jun 23, 2009 at 11:00 AM, Alfredo Alessandrini
alfreale74 at gmail.com
wrote:
Hi,
I've a vector like this:
inc
[1]
22 matches
Mail list logo