[R] insert/copy skipped lines to output

David Winsemius dwinsemius at comcast.net
Sun Mar 28 21:08:03 CEST 2010


On Mar 28, 2010, at 2:41 PM, Muhammad Rahiz wrote:

> something like this...
>
> x1 <- as.matrix(read.table("input.txt"))[1:6,] # reads only header
> x2 < -array(1:6,dim=c(2,3))
>
> write(x,"output.txt")

write() will process the matrix elements in their natural "vector"  
order.

> write 
> .table(x3,"output.txt",row.names=FALSE,col.names=FALSE,append=TRUE)
>
> But the results print;
>
> ncols
> nrows
> xllcorner
> yllcorner
> cellsize
> NODATA_value
>   180.00
>   290.00
> -200000.00
> -200000.00
>  5000.00
> -9999.00
> 1 3 5
> 2 4 6


?write.table

Add an appropriate , file= , to this and it should do what you expect

 > write.table(x1, row.names=FALSE, col.names=FALSE, quote=FALSE)

ncols     180
nrows     290
xllcorner -200000
yllcorner -200000
cellsize    5000
NODATA_value NA

>
>
>
>
> Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling
> Climate Research Laboratory, School of Geography & the Environment	
> Oxford University Centre for the Environment, University of Oxford
> South Parks Road, Oxford, OX1 3QY, United Kingdom
> Tel: +44 (0)1865-285194	 Mobile: +44 (0)7854-625974
> Email: muhammad.rahiz at ouce.ox.ac.uk
>
>
>
> David Winsemius wrote:
>> On Mar 28, 2010, at 1:11 PM, Muhammad Rahiz wrote:
>>
>>
>>> Yes, it works. Just wondering if the technique can be optimized...
>>>
>>
>> IN WHAT WAY????
>> --  David.
>>
>>> David Winsemius wrote:
>>>
>>>> On Mar 28, 2010, at 12:51 PM, Muhammad Rahiz wrote:
>>>>
>>>>
>>>>
>>>>> Dear Jannis,
>>>>>
>>>>> Thanks for the tip. It works but I'd like to improve on the way  
>>>>> I   did it.
>>>>>
>>>>> x <- array(1:50,dim=c(10,10)) # data
>>>>>
>>>>> h1 <- c("ncols                180") # header, line 1
>>>>> h2 <- c("nrows               290") # header, line 2
>>>>> h3 <- c("xllcorner           -200000") # header, line 3
>>>>> h4 <- c("yllcorner           -200000") # header, line 4
>>>>> h5 <- c("cellsize             5000") # header, line 5
>>>>> h6 <- c("NODATA_value  NA") # header, line 6
>>>>>
>>>>> h0 <- rbind(h1,h2,h3,h4,h5,h6)
>>>>>
>>>>> write(ho,"file.txt")
>>>>> write 
>>>>>  .table(x,"file.txt",row.names=FALSE,col.names=FALSE,append=TRUE)
>>>>>
>>>>> The header should display as;
>>>>>
>>>>> ncols                   180
>>>>> nrows                  290
>>>>> xllcorner              -200000
>>>>> yllcorner              -200000
>>>>> cellsize                 5000
>>>>> NODATA_value     NA
>>>>>
>>>>>
>>>> Executing that code appears to deliver what you requested. So   
>>>> rather  than asking us to read you mind, can you explain what is   
>>>> missing?
>>>>
>>>> ncols                180
>>>> nrows               290
>>>> xllcorner           -200000
>>>> yllcorner           -200000
>>>> cellsize             5000
>>>> NODATA_value  NA
>>>> 1 11 21 31 41 1 11 21 31 41
>>>> 2 12 22 32 42 2 12 22 32 42
>>>> 3 13 23 33 43 3 13 23 33 43
>>>> 4 14 24 34 44 4 14 24 34 44
>>>> 5 15 25 35 45 5 15 25 35 45
>>>> 6 16 26 36 46 6 16 26 36 46
>>>> 7 17 27 37 47 7 17 27 37 47
>>>> 8 18 28 38 48 8 18 28 38 48
>>>> 9 19 29 39 49 9 19 29 39 49
>>>> 10 20 30 40 50 10 20 30 40 50
>>>>
>>>>
>>>>
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list