Kanika,

You need to use "full.names = TRUE" in list files to get the path + file
name

compare:

files = list.files(path= "C:/Work/Climate stuff/Original_data/AR4_
climate_change_ASCII_grids",pattern="^tas_2040.*a1b.*hadcm3")

files[1]

files = list.files(path= "C:/Work/Climate stuff/Original_data/AR4_
climate_change_ASCII_grids",pattern="^tas_2040.*a1b.*hadcm3", full.names =
TRUE)

files[1]

now you can do:

r < -raster(files[1], crs=prj_latlong)
s <- stack(files)

Robert


On Thu, Mar 1, 2012 at 5:51 PM, Kanika Jhunjhnuwala <
JhunjhnuwalaK@landcareresearch.co.nz> wrote:

> Hi
>
> I have been all through google looking for an answer to this.
>
> I am trying to read a list of text files as rasters.
>
>
> >prj_latlong <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs "
> > list.files(path= "C:/Work/Climate
> stuff/Original_data/AR4_climate_change_ASCII_grids",pattern="^tas_2040.*a1b.*hadcm3")
> > b<-raster(a,crs=prj_latlong)
> Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
>  :
>  Cannot create a RasterLayer object from this file. (file does not exist)
> In addition: Warning messages:
> 1: In if (x == "" | x == ".") { :
>  the condition has length > 1 and only the first element will be used
> 2: In if (start != "http" & start != "ftp") { :
>  the condition has length > 1 and only the first element will be used
> 3: In if (fileext %in% c(".GRD", ".GRI")) { :
>  the condition has length > 1 and only the first element will be used
> 4: In if (!file.exists(x)) { :
>  the condition has length > 1 and only the first element will be used
> 5: In if (extension(x) == "") { :
>  the condition has length > 1 and only the first element will be used
> 6: In if (fileext %in% c(".NC", ".NCDF", ".NETCDF")) { :
>  the condition has length > 1 and only the first element will be used
> 7: In if (fileext == ".GRD") { :
>  the condition has length > 1 and only the first element will be used
> 8: In if (fileext %in% c(".SGRD", ".SDAT")) { :
>  the condition has length > 1 and only the first element will be used
> 9: In if (nchar(filename) == 0) stop("empty file name") :
>  the condition has length > 1 and only the first element will be used
> 10: In if (!file.exists(x)) { :
>  the condition has length > 1 and only the first element will be used
>
> also if I try this instead I get an error
>
> > hadcm3_40<-lapply(list.files(path=
> "Original_data/AR4_climate_change_ASCII_grids/",pattern="^tas_2040.*a1b.*hadcm3"),raster,
> crs=prj_latlong)
>
> Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
>  :
>
>  Cannot create a RasterLayer object from this file. (file does not exist)
>
>
> But if I do select a single file instead of the list and do this it works
> File<-
> raster("Original_data/AR4_climate_change_ASCII_grids/tas_2040a1b_apr_ukmo_hadcm3.txt",
> crs=prj_latlong)
>
> Somehow, it just cant read a list of text files as a list of rasters but
> can read a single one. I am not sure what to do. If I try different
> functions in lapply I get a list of 12 which is what I want (there are 12
> files I want to read in and make rasters from).
>
> Any help would be great
>
> Thanks
> KJ
>
>
> ________________________________
> Please consider the environment before printing this email
> Warning: This electronic message together with any attachments is
> confidential. If you receive it in error: (i) you must not read, use,
> disclose, copy or retain it; (ii) please contact the sender immediately by
> reply email and then delete the emails.
> The views expressed in this email may not be those of Landcare Research
> New Zealand Limited. http://www.landcareresearch.co.nz
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

