[R-sig-Geo] Change Lines'IDs within a SpatialLines object

Roger Bivand Roger.Bivand at nhh.no
Tue Feb 7 18:26:25 CET 2017


On Tue, 7 Feb 2017, marta azores wrote:

> Hello everyone,
>
> I would like to change the ID of SpatialLinesDataFrame (SLDF), by the value
> of one variable (in my case splndf$Idkey).
>
> I read the answer of Roger Bivand in this link:
> https://stat.ethz.ch/pipermail/r-sig-geo/2015-December/023809.html
>
> This works well when I write an specific number. However, I can't change
> the id of my SLDF by my Idkey value.
>
> At first the value in the SLDF was an integer and I converted to a numeric
> value. I don't understand why works with a number and not when I define the
> number from the SLDF.
> Any idea?

The values contained in row.names() of data.frame objects and similar are 
always seen as being of type character. Consequently, if you try to assign 
a vector of type numeric, it will fail.

Never, ever use @ - these are exclusively for programming internally. Use 
splndf$Idkey, which is its accessor method. Either use 
as.character(splndf$Idkey), or possibly formatC(splndf$Idkey, format="d", 
flag="0", width=<>) for left-padding of zeros to your choice of width if 
the column is integer but should be like "0010", and integer 10 is stored.

Roger

>
> Thanks in advance,
>
> Marta
> #######################################################################
> ##script
>
> ########change the ID's SLDF
>
> #Please use row.names(), so here:
> row.names(L1) <- c("c", "d")
> #If L1 was a SpatialLinesDataFrame, the row.names of the data slot would
> also
> #be updated.
> ###################################################
> row.names(splndf)
> #[1] "1"
>
> #1) First, I try manually to change the id (works)
> row.names(splndf) <- ("2905")
> str(splndf)#@ ID   : chr "2905"
> str(2905)
> #num 2905
> is.numeric(2905)
> 2905
> #2) Second, I try extract the Idkey value and convert into the SLDF's ID.
> k<-splndf at data$Idkey
> str(k)
> #int 2905
> k <- as.numeric(k)
> k
> #[1] 2905
> str(k)
> # num 2905
> is.numeric(k)
> #TRUE
> k
> row.names(splndf) <- (k)
> #Error in (function (classes, fdef, mtable)  :
> #unable to find an inherited method for function ‘spChFIDs’ for signature
> ‘"SpatialLinesDataFrame", "numeric"’
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
http://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
http://depsy.org/person/444584


More information about the R-sig-Geo mailing list