Re: [R] First value in a row

2012-07-25 Thread arun


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 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, 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 smartpink...@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.singm...@psychologie.uni-freiburg.de
 To: arun smartpink...@yahoo.com
 Cc: Camilo Mora cm...@dal.ca; R help r-help@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 cm...@dal.ca
 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
 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

Re: [R] First value in a row

2012-07-25 Thread arun
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, 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 smartpink...@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.singm...@psychologie.uni-freiburg.de
 To: arun smartpink...@yahoo.com
 Cc: Camilo Mora cm...@dal.ca; R help r-help@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 cm...@dal.ca
 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
 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@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

Re: [R] First value in a row

2012-07-25 Thread arun
 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 cm...@dal.ca
 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
 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@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
 
 
 
 

__
R-help@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.


Re: [R] First value in a row

2012-07-25 Thread arun
 
 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 cm...@dal.ca
 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
 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@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
 
 
 
 
 
 


__
R-help@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.


[R] First value in a row

2012-07-24 Thread Camilo Mora

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 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@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.


Re: [R] First value in a row

2012-07-24 Thread Henrik Singmann

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
, header = TRUE)

apply(dat[,-(1:2)], 1, function(x) x[!is.na(x)][1])

gives:
[1] 0.1 0.2 0.4

Cheers,
Henrik

Camilo Mora schrieb:

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 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@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.


Re: [R] First value in a row

2012-07-24 Thread 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 cm...@dal.ca
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
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@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.


__
R-help@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.


Re: [R] First value in a row

2012-07-24 Thread Henrik Singmann

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
0110  NA  NA  .1
0111  .4  NA  .3
0112  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
0110  NA  NA  .1
0111  NA  .2  .3
0112  .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 cm...@dal.ca
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
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 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@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

__
R-help@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.


Re: [R] First value in a row

2012-07-24 Thread William Dunlap
If there are a lot more rows than columns in your data.frame then looping
over columns is faster than looping over rows.   Comparing 
f2 - function (dataFrame) 
{
retval - dataFrame[, 1]
for (col in dataFrame[-1]) {
na - is.na(retval)
if (!any(na)) 
break
retval[na] - col[na]
}
retval
}
to the looping over row version
f1 - function (dataFrame)  apply(dataFrame, 1, function(x) x[!is.na(x)][1])

for a 250,000 row by 3 coludata.frame, f1 takes 2.48 seconds and f1 0.02.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap 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,
 
 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
 , header = TRUE)
 
 apply(dat[,-(1:2)], 1, function(x) x[!is.na(x)][1])
 
 gives:
 [1] 0.1 0.2 0.4
 
 Cheers,
 Henrik
 
 Camilo Mora schrieb:
  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 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@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.

__
R-help@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.


Re: [R] First value in a row

2012-07-24 Thread arun
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.singm...@psychologie.uni-freiburg.de
To: arun smartpink...@yahoo.com
Cc: Camilo Mora cm...@dal.ca; R help r-help@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 cm...@dal.ca
 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
 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@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


__
R-help@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.


Re: [R] First value in a row

2012-07-24 Thread Camilo Mora

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  .6
0112  .2  .3  NA
0111  .1  NA  NA
0110  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 smartpink...@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.singm...@psychologie.uni-freiburg.de
To: arun smartpink...@yahoo.com
Cc: Camilo Mora cm...@dal.ca; R help r-help@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 cm...@dal.ca
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
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@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





__
R-help@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.


Re: [R] First value in a row

2012-07-24 Thread Tyler Rinker

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] 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
 0112  .4  .5  .6
 0112  .2  .3  NA
 0111  .1  NA  NA
 0110  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 smartpink...@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
  0110  NA  NA  .1
  0111  .4  NA  .3
  0112  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
  0110  NA  NA  NA
  0111  NA  NA  .3
  0112  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  NANA
  #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.singm...@psychologie.uni-freiburg.de
  To: arun smartpink...@yahoo.com
  Cc: Camilo Mora cm...@dal.ca; R help r-help@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
  0110  NA  NA  .1
  0111  .4  NA  .3
  0112  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
  0110  NA  NA  .1
  0111  NA  .2  .3
  0112  .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 cm...@dal.ca
  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
  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 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@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help

[R] first value...

2009-06-23 Thread Alfredo Alessandrini
Hi,

I've a vector like this:

 inc
  [1]NANANANANANANA
  [8]NANANANANANANA
 [15]NANANANANANANA
 [22]NANANANANANANA
 [29]NANANANANANANA
 [36]NANANANANANANA
 [43]NANANANANANANA
 [50]NANANANANANANA
 [57]NANANANANANANA
 [64]NANANANANANANA
 [71]NANANANANANANA
 [78]NANANANA 13.095503 10.140119  7.989186
 [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
 [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
 [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169

I must obtain the position of first value of the vector...

In this case is 82.

 inc[82]
[1] 13.09550


Regards,

Alfredo

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Henrique Dallazuanna
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]NANANANANANANA
  [15]NANANANANANANA
  [22]NANANANANANANA
  [29]NANANANANANANA
  [36]NANANANANANANA
  [43]NANANANANANANA
  [50]NANANANANANANA
  [57]NANANANANANANA
  [64]NANANANANANANA
  [71]NANANANANANANA
  [78]NANANANA 13.095503 10.140119  7.989186
  [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
  [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
  [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169

 I must obtain the position of first value of the vector...

 In this case is 82.

  inc[82]
 [1] 13.09550


 Regards,

 Alfredo

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Alfredo Alessandrini
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
  [15]        NA        NA        NA        NA        NA        NA        NA
  [22]        NA        NA        NA        NA        NA        NA        NA
  [29]        NA        NA        NA        NA        NA        NA        NA
  [36]        NA        NA        NA        NA        NA        NA        NA
  [43]        NA        NA        NA        NA        NA        NA        NA
  [50]        NA        NA        NA        NA        NA        NA        NA
  [57]        NA        NA        NA        NA        NA        NA        NA
  [64]        NA        NA        NA        NA        NA        NA        NA
  [71]        NA        NA        NA        NA        NA        NA        NA
  [78]        NA        NA        NA        NA 13.095503 10.140119  7.989186
  [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
  [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
  [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169
 [106]  4.280065  5.942021  6.292010 11.866446 19.450442 11.942362  6.224328
 [113]  3.176050  5.456117  2.733487  3.992823 13.633171 19.514301 25.085256
 [120]  5.640089  5.890486 12.421150 18.821420 22.478664 11.503805  7.051254
 [127]  7.560921 12.000394 20.464875 16.147598 13.746290  9.416060 35.848221
 [134] 36.739481 23.516759  7.317599  3.928247 10.371437 11.202935 12.574649
 [141]  6.906980  9.191260  7.080267  2.810271  5.494705 10.617141 14.578020
 [148] 10.981610  7.343975  2.179511  2.726651 10.794842  9.872493        NA
 [155] 10.525064 16.134541 29.283385 18.352996  9.216318  6.253805  2.704267
 [162]  4.274514  3.138237 12.296835 20.982433 13.001104  2.606328  3.333271
 [169]  5.514425  2.179244  5.381514  6.848380  3.794428  5.114591  4.975830
 [176]  3.809948 10.131608 14.145913


 inc[82]
 [1] 13.09550

  which(diff(is.na(inc))  0)
 [1]  81 154

 inc[154]
 [1] NA


 I must obtain only the first value, in this case: inc[82].


 Regards,

 Alfredo




 2009/6/23 Henrique Dallazuanna www...@gmail.com

 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]        NA        NA        NA        NA        NA        NA        NA
  [8]        NA        NA        NA        NA        NA        NA        NA
  [15]        NA        NA        NA        NA        NA        NA        NA
  [22]        NA        NA        NA        NA        NA        NA        NA
  [29]        NA        NA        NA        NA        NA        NA        NA
  [36]        NA        NA        NA        NA        NA        NA        NA
  [43]        NA        NA        NA        NA        NA        NA        NA
  [50]        NA        NA        NA        NA        NA        NA        NA
  [57]        NA        NA        NA        NA        NA        NA        NA
  [64]        NA        NA        NA        NA        NA        NA        NA
  [71]        NA        NA        NA        NA        NA        NA        NA
  [78]        NA        NA        NA        NA 13.095503 10.140119  7.989186
  [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
  [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
  [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169

 I must obtain the position of first value of the vector...

 In this case is 82.

  inc[82]
 [1] 13.09550


 Regards,

 Alfredo

 __
 R-help@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.



 --
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O



 --

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread David Winsemius

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]NANANANANA 
NANA
[22]NANANANANA 
NANA
[29]NANANANANA 
NANA
[36]NANANANANA 
NANA
[43]NANANANANA 
NANA
[50]NANANANANA 
NANA
[57]NANANANANA 
NANA
[64]NANANANANA 
NANA
[71]NANANANANA 
NANA
[78]NANANANA 13.095503 10.140119   
7.989186
[85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785  
14.421302
[92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543   
2.005056
[99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902  
18.762169


I must obtain the position of first value of the vector...

In this case is 82.


inc[82]

[1] 13.09550



which(!is.na(inc))[1]



Regards,

Alfredo



David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Alfredo Alessandrini
I've the NA value also between the value of the vector:

 inc
  [1]NANANANANANANA
  [8]NANANANANANANA
 [15]NANANANANANANA
 [22]NANANANANANANA
 [29]NANANANANANANA
 [36]NANANANANANANA
 [43]NANANANANANANA
 [50]NANANANANANANA
 [57]NANANANANANANA
 [64]NANANANANANANA
 [71]NANANANANANANA
 [78]NANANANA 13.095503 10.140119  7.989186
 [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
 [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
 [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169
[106]  4.280065  5.942021  6.292010 11.866446 19.450442 11.942362  6.224328
[113]  3.176050  5.456117  2.733487  3.992823 13.633171 19.514301 25.085256
[120]  5.640089  5.890486 12.421150 18.821420 22.478664 11.503805  7.051254
[127]  7.560921 12.000394 20.464875 16.147598 13.746290  9.416060 35.848221
[134] 36.739481 23.516759  7.317599  3.928247 10.371437 11.202935 12.574649
[141]  6.906980  9.191260  7.080267  2.810271  5.494705 10.617141 14.578020
[148] 10.981610  7.343975  2.179511  2.726651 10.794842  9.872493NA
[155] 10.525064 16.134541 29.283385 18.352996  9.216318  6.253805  2.704267
[162]  4.274514  3.138237 12.296835 20.982433 13.001104  2.606328  3.333271
[169]  5.514425  2.179244  5.381514  6.848380  3.794428  5.114591  4.975830
[176]  3.809948 10.131608 14.145913


 inc[82]
[1] 13.09550

  which(diff(is.na(inc))  0)
[1]  81 154

 inc[154]
[1] NA


I must obtain only the first value, in this case: inc[82].


Regards,

Alfredo




2009/6/23 Henrique Dallazuanna www...@gmail.com

 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]        NA        NA        NA        NA        NA        NA        NA
  [8]        NA        NA        NA        NA        NA        NA        NA
  [15]        NA        NA        NA        NA        NA        NA        NA
  [22]        NA        NA        NA        NA        NA        NA        NA
  [29]        NA        NA        NA        NA        NA        NA        NA
  [36]        NA        NA        NA        NA        NA        NA        NA
  [43]        NA        NA        NA        NA        NA        NA        NA
  [50]        NA        NA        NA        NA        NA        NA        NA
  [57]        NA        NA        NA        NA        NA        NA        NA
  [64]        NA        NA        NA        NA        NA        NA        NA
  [71]        NA        NA        NA        NA        NA        NA        NA
  [78]        NA        NA        NA        NA 13.095503 10.140119  7.989186
  [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
  [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
  [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169

 I must obtain the position of first value of the vector...

 In this case is 82.

  inc[82]
 [1] 13.09550


 Regards,

 Alfredo

 __
 R-help@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.



 --
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O



--
--
Alfredo Alessandrini, Dr.
Post-doc
Department of Environment and Forest (DAF)
Unitus - Viterbo
http://www.unitus.it/dipartimenti/daf/dendro/

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Wacek Kusnierczyk

Alfredo Alessandrini wrote:

Hi,

I've a vector like this:

  

inc


  [1]NANANANANANANA
  [8]NANANANANANANA
 [15]NANANANANANANA
 [22]NANANANANANANA
 [29]NANANANANANANA
 [36]NANANANANANANA
 [43]NANANANANANANA
 [50]NANANANANANANA
 [57]NANANANANANANA
 [64]NANANANANANANA
 [71]NANANANANANANA
 [78]NANANANA 13.095503 10.140119  7.989186
 [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
 [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
 [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169

I must obtain the position of first value of the vector...

In this case is 82.
  


one way:

   which(!is.na(inc))[1]
   # 82


vQ

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Philipp Pagel
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]NANANANANANANA
  [22]NANANANANANANA
  [29]NANANANANANANA
  [36]NANANANANANANA
  [43]NANANANANANANA
  [50]NANANANANANANA
  [57]NANANANANANANA
  [64]NANANANANANANA
  [71]NANANANANANANA
  [78]NANANANA 13.095503 10.140119  7.989186
  [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
  [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
  [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169
 
 I must obtain the position of first value of the vector...
 
 In this case is 82.
 
  inc[82]
 [1] 13.09550

which(!is.na(inc))[1]

cu
Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://webclu.bio.wzw.tum.de/~pagel/

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Daniel Malter
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 Alfredo Alessandrini
Gesendet: Tuesday, June 23, 2009 10:00 AM
An: r-help@r-project.org
Betreff: [R] first value...

Hi,

I've a vector like this:

 inc
  [1]NANANANANANANA
  [8]NANANANANANANA
 [15]NANANANANANANA
 [22]NANANANANANANA
 [29]NANANANANANANA
 [36]NANANANANANANA
 [43]NANANANANANANA
 [50]NANANANANANANA
 [57]NANANANANANANA
 [64]NANANANANANANA
 [71]NANANANANANANA
 [78]NANANANA 13.095503 10.140119  7.989186
 [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
[92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
[99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169

I must obtain the position of first value of the vector...

In this case is 82.

 inc[82]
[1] 13.09550


Regards,

Alfredo

__
R-help@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.

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Henrique Dallazuanna
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]NANANANANANANA
  [15]NANANANANANANA
  [22]NANANANANANANA
  [29]NANANANANANANA
  [36]NANANANANANANA
  [43]NANANANANANANA
  [50]NANANANANANANA
  [57]NANANANANANANA
  [64]NANANANANANANA
  [71]NANANANANANANA
  [78]NANANANA 13.095503 10.140119  7.989186
  [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 14.421302
  [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543  2.005056
  [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 18.762169
 [106]  4.280065  5.942021  6.292010 11.866446 19.450442 11.942362  6.224328
 [113]  3.176050  5.456117  2.733487  3.992823 13.633171 19.514301 25.085256
 [120]  5.640089  5.890486 12.421150 18.821420 22.478664 11.503805  7.051254
 [127]  7.560921 12.000394 20.464875 16.147598 13.746290  9.416060 35.848221
 [134] 36.739481 23.516759  7.317599  3.928247 10.371437 11.202935 12.574649
 [141]  6.906980  9.191260  7.080267  2.810271  5.494705 10.617141 14.578020
 [148] 10.981610  7.343975  2.179511  2.726651 10.794842  9.872493NA
 [155] 10.525064 16.134541 29.283385 18.352996  9.216318  6.253805  2.704267
 [162]  4.274514  3.138237 12.296835 20.982433 13.001104  2.606328  3.333271
 [169]  5.514425  2.179244  5.381514  6.848380  3.794428  5.114591  4.975830
 [176]  3.809948 10.131608 14.145913
 

  inc[82]
 [1] 13.09550

   which(diff(is.na(inc))  0)
 [1]  81 154

  inc[154]
 [1] NA


 I must obtain only the first value, in this case: inc[82].


 Regards,

 Alfredo




 2009/6/23 Henrique Dallazuanna www...@gmail.com
 
  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]NANANANANANA
  NA
   [8]NANANANANANA
  NA
   [15]NANANANANANA
  NA
   [22]NANANANANANA
  NA
   [29]NANANANANANA
  NA
   [36]NANANANANANA
  NA
   [43]NANANANANANA
  NA
   [50]NANANANANANA
  NA
   [57]NANANANANANA
  NA
   [64]NANANANANANA
  NA
   [71]NANANANANANA
  NA
   [78]NANANANA 13.095503 10.140119
  7.989186
   [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785
 14.421302
   [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543
  2.005056
   [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902
 18.762169
 
  I must obtain the position of first value of the vector...
 
  In this case is 82.
 
   inc[82]
  [1] 13.09550
 
 
  Regards,
 
  Alfredo
 
  __
  R-help@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.
 
 
 
  --
  Henrique Dallazuanna
  Curitiba-Paraná-Brasil
  25° 25' 40 S 49° 16' 22 O



 --
 --
 Alfredo Alessandrini, Dr.
 Post-doc
 Department of Environment and Forest (DAF)
 Unitus - Viterbo
 http://www.unitus.it/dipartimenti/daf/dendro/
 --




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Stavros Macrakis
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:

 which(diff(is.na(xx))0)
[1] 3 6

   -s

On Tue, Jun 23, 2009 at 10:00 AM, Alfredo Alessandrini alfreal...@gmail.com
 wrote:

 Hi,

 I've a vector like this:

  inc
  [1]NANANANANANA
  NA...
 [71]NANANANANANANA
  [78]NANANANA 13.095503 10.140119  7.989186

...

 I must obtain the position of first value of the vector...

 In this case is 82.

  inc[82]
 [1] 13.09550


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Petr PIKAL
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 
alfreal...@gmail.com
  wrote:
 
  Hi,
 
  I've a vector like this:
 
   inc
   [1]NANANANANANA  NA
   [8]NANANANANANA  NA
   [15]NANANANANANA   NA
   [22]NANANANANANA   NA
   [29]NANANANANANA   NA
   [36]NANANANANANA   NA
   [43]NANANANANANA   NA
   [50]NANANANANANA   NA
   [57]NANANANANANA   NA
   [64]NANANANANANA   NA
   [71]NANANANANANA   NA
   [78]NANANANA 13.095503 10.140119 
7.989186
   [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785 
14.421302
   [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543 
2.005056
   [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902 
18.762169
 
  I must obtain the position of first value of the vector...
 
  In this case is 82.
 
   inc[82]
  [1] 13.09550
 
 
  Regards,
 
  Alfredo
 
  __
  R-help@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.
 
 
 
 
 -- 
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O
 
[[alternative HTML version deleted]]
 
 __
 R-help@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.

__
R-help@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.


Re: [R] first value...

2009-06-23 Thread Carl Witthoft


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]NANANANANANA  NA
   [8]NANANANANANA  NA
   [15]NANANANANANA   NA
   [22]NANANANANANA   NA
   [29]NANANANANANA   NA
   [36]NANANANANANA   NA
   [43]NANANANANANA   NA
   [50]NANANANANANA   NA
   [57]NANANANANANA   NA
   [64]NANANANANANA   NA
   [71]NANANANANANA   NA
   [78]NANANANA 13.095503 10.140119
7.989186
   [85]  8.711888  7.201234 13.029250 14.430755  8.662832  8.810785
14.421302
   [92]  7.614985  7.548091  9.843389 14.977402 20.875255  7.787543
2.005056
   [99]  4.016916  3.601773  4.140390  7.241999 13.280794 18.038902
18.762169
 
  I must obtain the position of first value of the vector...
 
  In this case is 82.
 
   inc[82]
  [1] 13.09550

__
R-help@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.