[R-sig-Geo] loop xy cbind
marta azores
martazores at gmail.com
Mon Dec 5 17:34:13 CET 2016
2016-12-05 12:52 GMT-01:00 marta azores <martazores at gmail.com>:
> Hi everyone,
> I would like to create a loop to split my dataframe with locations by
> transect, , with the objective to get 2 lines, one for each transect .
> I have this script to achieve that... but the results are different that I
> want. I only get the first position (x,y).
>
> Anyone has an idea to fix that?
> Thanks in advance
>
> Marta
>
>
>
> #script
> #
> ###################################
> #boat <- read.table("C:/mini.csv", header=TRUE, sep=",", na.strings="NA",
> #dec=".",strip.white=TRUE)#
>
> library(plyr)
> library(loop)
>
> boat
>
> # Idkey IdkeyL Long1 Lat1 Date Hour MiniSurvey Sort1 Year
> #1 1968 1968L -28.64475 38.67035 40302 0.7291667 25595 1 2010
> #2 1968 1968L -28.72917 38.66908 40302 0.7437500 25595 2 2010
> #3 1968 1968L -28.74920 38.67173 40302 0.7479167 25595 3 2010
> #4 1969 1969L -28.70907 38.66965 40302 0.7562500 25595 4 2010
> #5 1969 1969L -28.63157 38.64717 40302 0.7715278 25596 5 2010
> #6 1969 1969L -28.87397 38.26693 40303 0.3340278 25597 6 2010
> boat_split <- split(boat, boat$Idkey)
> boat_xy<-(boat_split, cbind(boat$Long1,boat$Lat1))
>
> summary(boat_split)
> # Length Class Mode
> #1968 9 data.frame list
> #1969 9 data.frame list
>
> new_names <- c("1968","1969")
> for (i in 1:length(boat_split)) {
> assign(new_names[i], boat_split[[i]]);
> boat_xy<-cbind(boat$Long1[[i]],boat$Lat1[[i]])}
>
> boat_xy#results
> # [,1] [,2]
> #[1,] -28.72917 38.66908
>
> #However, I was expecting a list of dataframes by Idkey
> (c("1968","1969")), with the coordinates, something like that:
> $`1968`
> -28.72917 38.66908
> -28.72917 38.66908
> -28.74920 38.67173
> $`1969`
> -28.70907 38.66965
> -28.63157 38.64717
> -28.87397 38.26693
> class(boat_split)#list
>
> #to get lines : apply this function
> library(sp)
> Sl1 <- Line(boat_xy)
>
>
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list