[R] using rbind in a for loop
jim holtman
jholtman at gmail.com
Tue Feb 2 18:34:17 CET 2010
This might give you a hint as to how to do it since I have no idea of
what your data looks like:
lon <- lat <- matrix(1:100, nrow=5)
output <- file('/tempxx.txt', open='w')
for (i in 1:5){
x <- cbind(lon[,i], lat[,i])
write.table(x, row.names=FALSE, col.names=FALSE, file=output)
cat('>\n', file=output)
}
close(output)
This gives a file like:
1 1
2 2
3 3
4 4
5 5
>
6 6
7 7
8 8
9 9
10 10
>
11 11
12 12
13 13
14 14
15 15
>
16 16
17 17
18 18
19 19
20 20
>
21 21
22 22
23 23
24 24
25 25
>
On Mon, Feb 1, 2010 at 11:16 PM, Eunjung Kim <eunjung.hawaii at gmail.com> wrote:
> *Dear R users,
> *I'm facing a trivial problem but I cannot solve it.
> My question is:
> I want to make data set like this.
>>
> i_lon1 i_lat1
> i_lon2 i_lat2
> i_lon3 i_lat3
> i_lon4 i_lat4
>>
> i+1_lon1 i+1_lat1
> i+1_lon2 i+1_lat2
> i+1_lon3 i+1_lat3
> i+1_lon4 i+1_lat4
>>
> i+2_lon1 i+2_lat1
> i+2_lon2 i+2_lat2
> i+2_lon3 i+2_lat3
> i+2_lon4 i+2_lat4
> .
> .
> .
>>
> i+5_lon1 i+5_lat1
> i+5_lon2 i+5_lat2
> i+5_lon3 i+5_lat3
> i+5_lon4 i+5_lat4
>
> My code is
> for (i in 1:5) {
> j <- which(!is.na(lat[i,]))
> p <- cbind(lon[i,j],lat[i,j])
> cat(c(">",p),sep=" ",file="data.dat",append=T)}
>
> and what I got is even not close to what I want: many lines of empty and
> whole bunch of numbers without line change.
>
> How can I solve this?
>
> Thanks in advance,
> EJ
> --
> <><<><<><<><<><<><<><<><<><
> Eunjung Kim
> Department of Oceanography
> University of Hawaii at Manoa
> 1000 Pope Road
> Honolulu, HI 96822
> United States
>><>><>><>><>><>><>><>><>><>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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