-1

I have many .txt file that looks like this:

13129 13161 13128 13134 13186 13224 13180 13140 13116 13098 13100 13146 13169 13185 13199 13230 13204 13160 13141 13113 13136 13137 13158 13202 13229 13257 13239 13192 13142 13114 13146 13145 13136 13157 13199 13208 13224 13220 13191 13151 13128 13126 13139 13150 13178 13206 13229 13225 13221 13190 13111 13119 13136 13166 13200 13229 13252 13235 13225 13201 13107 13131 13154 13172 13195 13232 13202 13233 13232 13237

This is data converted from a raster in Arcmap, but I only need 23 specific values i.e. first row, fourth column. How can I insert the values I want into r? and how can this be repeated multiple times?

Ok what I was meant to say is that each file represents a certain day so i need to extract the specific cells and put them in rows like so

        cell1    cell2 ..............................................cell 23
  day 1 13129   13161  ..............................................13126 
  day 2
  day 3
  etc 
  day n

so I would end up with a table 23 columns and n rows.

  • What’s wrong with reading the txt file and then selecting the values from the object? – Hugh Mar 14 '18 at 12:53
  • You just need to read the data in and subset what you want. It's a bit unclear exactly how you want to specify which values to extract or how you want to repeat this multiple times. When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 14 '18 at 12:53

1 Answers1

0

A general approach with raster format files in R is:

library(raster)
r <- raster("filename")

Where the file can be most raster types such as geotif or ESRI Grid. You have exported to ASCII. That works but it is not great, as it can slow things down a lot.

To extract some values, i.e. row 1, col 4

r[1, 4]

To get a cell value for multiple files, make a RasterStack

ff <- list.files(pattern="asc$")
s <- stack(ff)
s[1, 4]

You can learn more about spatial data in R here

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63