[R] store and repeat data based on row names (loop, if statement)

jim holtman jholtman at gmail.com
Mon May 31 16:37:10 CEST 2010


try this:

> x <- read.table(textConnection("TO      DIST    ID
+ 1       2.63981 'A1'
+ 2       0                  'A1'
+ 3       6.95836 'A1'
+ 4       8.63809 'A1'
+ 1       0                  'A1.1'
+ 2       2.63981 'A1.1'
+ 3       8.03071 'A1.1'
+ 4       8.90896 'A1.1'
+ 1       8.90896 'A2'
+ 2       8.63809 'A2'
+ 3       2.85602 'A2'
+ 4       0                  'A2'
+ 1       8.03071 'A2.1'
+ 2       6.95836 'A2.1'
+ 3       0                  'A2.1'
+ 4       2.85602 'A2.1'"), header=TRUE, as.is=TRUE)
> closeAllConnections()
> indx <- subset(x, DIST == 0)
> x$newCol <- indx$ID[match(x$TO, indx$TO)]
>
>
> x
   TO    DIST   ID newCol
1   1 2.63981   A1   A1.1
2   2 0.00000   A1     A1
3   3 6.95836   A1   A2.1
4   4 8.63809   A1     A2
5   1 0.00000 A1.1   A1.1
6   2 2.63981 A1.1     A1
7   3 8.03071 A1.1   A2.1
8   4 8.90896 A1.1     A2
9   1 8.90896   A2   A1.1
10  2 8.63809   A2     A1
11  3 2.85602   A2   A2.1
12  4 0.00000   A2     A2
13  1 8.03071 A2.1   A1.1
14  2 6.95836 A2.1     A1
15  3 0.00000 A2.1   A2.1
16  4 2.85602 A2.1     A2
>


On Mon, May 31, 2010 at 3:28 AM, RCulloch <ross.culloch at dur.ac.uk> wrote:
>
> Hello fellow R users,
>
> I have an issue that has me a little confused - sorry if the subject makes
> little sense, I wasn't sure how to refer to this problem. I have a data set
> I've extracted from ArcInfo (a section is shown below). It is spatial data,
> showing the distance from one ID to another. I want to get the actual 'TO'
> ID from the data set (there is no easy way to do this in Arc so I thought I
> would try in R). The way to do this is to find the dist = 0 row for an ID
> then that is that IDs unique 'TO' code, so if you look down the second
> column the highest no. is 4, and A1 = 2, A1.1 = 1, A2 = 4, A2.1 = 3. So I
> need to get that data and then put it in a new column that will basically
> read A1.1, A1, A2.1, A2, A1.1, A1, A2.1, A2, A1.1, A1, A2.1, A2, A1.1, A1,
> A2.1, A2.
>
> If anyone has any hints or tips or places to look I would be most grateful!
>
> Cheers,
>
> Ross
>
>
> TO      DIST    ID
> 1       2.63981 'A1'
> 2       0                  'A1'
> 3       6.95836 'A1'
> 4       8.63809 'A1'
> 1       0                  'A1.1'
> 2       2.63981 'A1.1'
> 3       8.03071 'A1.1'
> 4       8.90896 'A1.1'
> 1       8.90896 'A2'
> 2       8.63809 'A2'
> 3       2.85602 'A2'
> 4       0                  'A2'
> 1       8.03071 'A2.1'
> 2       6.95836 'A2.1'
> 3       0                  'A2.1'
> 4       2.85602 A2.1'
> --
> View this message in context: http://r.789695.n4.nabble.com/store-and-repeat-data-based-on-row-names-loop-if-statement-tp2236928p2236928.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list