1
$\begingroup$

I have to following data frame.

    x1     x2          x3
    0   58.33546    15.14554
    0   58.33604    15.14571
    0   58.33645    15.14581
    0   58.33681    15.14591
    0   58.33722    15.14600
    0   58.33810    15.14604
    0   58.33900    15.14599
    0   58.33966    15.14578

Consisting of long and lat, each pair belongs to a string (0,1,3,4 and so on, only the string 0 is displayed above).

I would like to first construct a new column with the pairs (x2,x3). How do I do that?

The coordinates above corresponding to 0 is actually a journey (say X), if there is another journey (Y) whose first coordinate is the same as X last, I would like to merge the sequence X and Y coordinates together and call that journey X (the continuation of X simply).

My data set is called data, for some reason merging them together wont work with

data[, x4 := paste(data$x2, data$x3)]

This will create a new column, only isplaying x2.

$\endgroup$
1
$\begingroup$

it probably depends on the use of your new generated column. Here I try to reproduce your example:

x1 <- c(0, 0)
x2 <- c(58.33546, 58.33604)
x3 <- c(15.14554, 15.14571)
df <- data.frame(x1, x2, x3)
df$x4 <- paste("(", as.character(df$x2), ", ", as.character(df$x3), ")")

And df$x4 will look like:

[1] "( 58.33546 ,  15.14554 )" "( 58.33604 ,  15.14571 )"

If you want to show the coordinates, the above format should be fine. If you want to do further analysis with that column, some other transformations are required.

| improve this answer | |
$\endgroup$
0
$\begingroup$

data.table supports list type columns. When doing assignments you need to put them in an additional list() or use `:=`(newcol=listvalue) syntax instead of newcol:=listvalue to avoid simplification when lists have a single element.

library(data.table)
data<-as.data.table(data)
data[, `:=`(x2x3=mapply(c, x2,x3, SIMPLIFY=F))]
data[, .(path=list(x1x2)), by=x1]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.