我有一张这样的表:
Stroke ID (starttime) sx sy time Point ID
<dbl> <dbl> <dbl> <dbl> <int>
1 0 667.750 824.250 0 1
2 0 668.500 824.500 53 2
3 10412 231.250 602.250 10412 3
4 10412 194.375 526.500 10852 4
5 11726 226.125 603.500 11726 5
6 11726 212.250 584.000 14766 6
7 16355 422.125 665.625 16355 7
8 16355 468.250 576.500 16635 8
9 17103 406.750 647.625 17103 9
10 17103 420.000 663.875 18089 10
sx 和 sy 是我想要转换为一个点列的坐标列。在此之前,我想在之后在此列中的点之间应用欧几里德距离函数(如sqrt(sum((point1-point2)^2)))
,因此该列必须保持数字。我到目前为止所做的是:< / p>
sxvector <- mydf[['sx']]
syvector <- mydf[['sy']]
points <- paste(sxvector, syvector, sep=" ")
给了我
[1] "667.75 824.25" "668.5 824.5" "231.25 602.25" "194.375 526.5"
[5] "422.125 665.625" "468.25 576"
...
看起来很不错,但当然现在是character
类型。
当我尝试使用as.numeric
或factor()
或unclass()
或其中任意组合将其转换为数字时,我最终只会使用NA。所以我认为这是一个负担(如果你证明我错了,我会很高兴的!)。然后我去了
coordframe <- data.frame(mydf$sx, mydf$sy)
coordlist <- c(t(coordframe))
再次给我一个好看的结果,如
[1] 667.750 824.250 668.500 824.500 231.250 602.250 194.375 526.500 226.125
...
仍然是numeric
。现在我想创建一个我可以放入dataframe
的列,它总是将两个后面的值组合为一个向量作为一行,它仍然是数字。例如:
Points
1 667.750 824.250
2 668.500 824.500
...
为实现这一目标,我使用了
split(coordlist, ceiling(seq_along(coordlist)/2))
我得到的东西看起来像这样
$`1`
[1] 667.75 824.25
$`2`
[1] 668.5 824.5
...
但是这些东西都是类型列表,我也无法将其转换为数字,并且无法进行计算。
知道我是如何管理的吗?我错过了一些微不足道的事吗? 编辑:我在下面添加了我的数据
structure(list(Stroke.ID..starttime. = c(0, 0, 10412, 10412,
11726, 11726, 16355, 16355, 17103, 17103, 21190, 21190, 21390,
21390, 21596, 21596, 21839, 21839, 22433, 22433, 22589, 22589,
23603, 23603, 23848, 23848, 24028, 24028, 24199, 24199, 24607,
24607, 24805, 24805, 24939, 24939, 25081, 25081, 25253, 25253,
25464, 25464, 43614, 43614, 44773, 44773, 47376, 47376, 50808,
50808, 51943, 51943, 53701, 53701, 54610, 54610, 56108, 56108,
56804, 56804, 57054, 57054, 58576, 58576, 59118, 59118, 59444,
59444, 59824, 59824, 60122, 60122, 62233, 62233, 63029, 63029,
64355, 64355, 69213, 69213, 69509, 69509, 69722, 69722, 69892,
69892, 70013, 70013, 70157, 70157, 71192, 71192, 71479, 71479,
72044, 72044, 72537, 72537, 72837, 72922, 72922, 73260, 73466,
73466, 73650, 73787, 73787, 81336, 81336, 81460, 81460, 81766,
81766, 82220, 82220, 82266, 82266, 82545, 82545, 85603, 85603,
92315, 92315, 92493, 92493, 92877, 92877, 93045, 93045, 93171,
93171, 93460, 93460, 103099, 103099, 104463, 104463, 114168,
114168, 117497, 117497, 117816, 117816, 118039, 118039, 118268,
118268, 118490, 118490, 118696, 118696, 118886, 118886, 119084,
119084, 119269, 119269, 119434, 119434, 119578, 119578, 119741,
119741, 119924, 119924, 120312, 120312, 120497, 120497, 120682,
120682, 120861, 120861, 121007, 121007, 121174, 121174, 121316,
121316, 121475, 121475, 121623, 121623, 121767, 121767, 121916,
121916, 122089, 122089, 122214, 122214, 122640, 122640, 129695,
129695, 129952, 129952, 130387, 130387, 130792, 130880, 130880,
131388, 131388, 151617, 151617, 152471, 152471, 154769, 154769,
155339, 155339, 155686, 155686, 155873, 155873, 156133, 156133,
156287, 156287, 156395, 156395, 170859, 170859), sx = c(667.75,
668.5, 231.25, 194.375, 226.125, 212.25, 422.125, 468.25, 406.75,
420, 471.125, 472.75, 462.625, 479.25, 477.75, 482.625, 488.75,
498.875, 493.375, 494.5, 505.875, 511.25, 489.25, 486, 495.625,
498.625, 497, 500, 508, 513, 512.75, 522.375, 516.25, 516.625,
519.5, 522.375, 519.125, 520.25, 523.75, 528.75, 530.125, 538,
264.5, 258.25, 256.75, 49, 63, 278.75, 65, 55.25, 64.375, 203.375,
43.375, 24.375, 34.125, 134.25, 28.375, 26.875, 28.375, 27.25,
45.5, 58.625, 35.5, 38.25, 38.75, 41.75, 37.625, 44.375, 42.625,
47.5, 53.75, 59.375, 24.5, 23, 55.625, 96.5, 34, 96, 305.25,
305.25, 309.875, 312, 309, 310.875, 316.625, 318.125, 313.75,
320.625, 321.75, 321.625, 348.25, 350.25, 356.625, 361.75, 311.125,
323.375, 327.75, 332.75, 331.5, 334.75, 343.75, 342, 349.25,
356.5, 350.875, 358.375, 375.5, 325, 331.5, 329.5, 337.375, 337.875,
346.375, 348.75, 348.375, 347.125, 356.125, 355.5, 373.25, 315.875,
359.125, 35.375, 45.875, 36, 46.875, 40.875, 49.125, 36.75, 35.625,
50, 54.375, 47.375, 57.75, 210, 85.25, 239, 92.5, 280.375, 467.625,
12.125, 30.75, 39.375, 55.5, 66.75, 80.125, 90.75, 104.5, 108.625,
117.75, 123.25, 136, 144.25, 156.875, 165.5, 178, 187.375, 196.25,
205.875, 213.875, 221, 230.75, 235.5, 249.75, 260.875, 265.75,
277.375, 287.125, 292.375, 301.125, 312, 319.75, 334.5, 343.5,
353.875, 364.5, 378.875, 389.375, 401, 412.25, 422.625, 431.25,
440.25, 445.875, 451.25, 456.125, 460.375, 461.75, 464.25, 465.5,
463.625, 463.875, 453.375, 471.5, 412, 414.125, 407.25, 414,
424.625, 434.375, 430.875, 435.75, 449.5, 443.25, 450.5, 159.5,
188.125, 153.875, 186, 153.875, 148.875, 150.75, 162.125, 159,
160, 153.125, 155.125, 148.375, 160.125, 154.5, 154.125, 158.75,
164.25, 18.375, 18.375), sy = c(824.25, 824.5, 602.25, 526.5,
603.5, 584, 665.625, 576.5, 647.625, 663.875, 621.875, 635.125,
620.125, 615.125, 624.75, 620.75, 624.25, 624.625, 616.875, 616.125,
624, 620.25, 640, 643, 629.125, 638.625, 637.75, 636.375, 631.75,
636, 625.125, 632.875, 628.875, 629.125, 628, 633.5, 626.5, 624.75,
627.75, 623.25, 624.25, 628.125, 422.5, 422.125, 436, 343, 307.125,
426.5, 497, 361.375, 308.375, 114.5, 569.625, 405.75, 295, 117.625,
285.75, 282.75, 311.875, 313.125, 241, 227.375, 227, 215.375,
210.25, 201, 194, 193.625, 180.625, 186.375, 170.875, 168.375,
356.75, 375.375, 345.875, 625.375, 547.25, 705.125, 522.25, 539.25,
529.875, 537.625, 523.25, 520.375, 526.375, 537.125, 527.875,
527.375, 524.375, 540.125, 520.125, 517.625, 506.75, 527.625,
589, 586.375, 585.5, 579.75, 573.625, 575.125, 576.5, 570.125,
561.25, 575.375, 570.5, 571.125, 557.5, 509.125, 504.875, 502.875,
501.875, 497.875, 499.875, 500.125, 498, 494.25, 491, 496.25,
482.375, 511.375, 506.125, 347.375, 349.25, 356, 331.125, 324.875,
327.125, 326, 324.625, 319.625, 308.625, 300.125, 303.75, 355.625,
258.125, 376.375, 240.5, 407.375, 571.5, 319.25, 320.125, 322.75,
325.375, 326.625, 329.375, 331, 336.75, 334.875, 339, 338.5,
344.25, 345.125, 351.625, 352.25, 358.5, 361.25, 367.625, 373,
379, 379.625, 388.125, 393.125, 407.625, 415.25, 423.875, 419.875,
418, 414.375, 414.375, 414.375, 413.75, 411.5, 411.375, 410.25,
411.125, 412.625, 412.75, 415.5, 420.5, 425, 433.625, 442.625,
450.375, 462, 472.875, 484.625, 501.75, 517.375, 530.25, 544.875,
560.125, 553.25, 551.875, 380.75, 395.625, 381, 391, 385.125,
387.875, 378, 385.5, 382.125, 381.375, 377.75, 618.875, 492.5,
615.875, 491.875, 621.5, 605.25, 575.875, 578.5, 570.5, 567.25,
555.5, 561.75, 549.375, 553.5, 552.875, 550, 546.25, 512, 17.625,
17.5), time = c(0, 53, 10412, 10852, 11726, 14766, 16355, 16635,
17103, 18089, 21190, 21270, 21390, 21470, 21596, 21769, 21839,
22345, 22433, 22486, 22589, 22749, 23603, 23736, 23848, 23914,
24028, 24081, 24199, 24492, 24607, 24727, 24805, 24898, 24939,
25019, 25081, 25134, 25253, 25386, 25464, 25677, 43614, 43734,
44773, 46319, 47376, 49082, 50808, 51354, 51943, 52676, 53701,
54234, 54610, 55010, 56108, 56374, 56804, 56830, 57054, 57120,
58576, 59016, 59118, 59371, 59444, 59750, 59824, 60024, 60122,
60428, 62233, 62379, 63029, 64002, 64355, 64848, 69213, 69439,
69509, 69602, 69722, 69762, 69892, 69945, 70013, 70079, 70157,
70490, 71192, 71405, 71479, 71559, 72044, 72377, 72537, 72670,
72837, 72922, 73148, 73260, 73466, 73559, 73650, 73787, 74120,
81336, 81362, 81460, 81726, 81766, 82032, 82220, 82233, 82266,
82346, 82545, 82878, 85603, 85909, 92315, 92395, 92493, 92759,
92877, 92983, 93045, 93098, 93171, 93397, 93460, 93860, 103099,
103859, 104463, 105183, 114168, 116248, 117497, 117630, 117816,
117896, 118039, 118119, 118268, 118348, 118490, 118570, 118696,
118762, 118886, 118952, 119084, 119150, 119269, 119322, 119434,
119487, 119578, 119644, 119741, 119807, 119924, 119977, 120312,
120378, 120497, 120563, 120682, 120735, 120861, 120914, 121007,
121073, 121174, 121227, 121316, 121382, 121475, 121528, 121623,
121676, 121767, 121833, 121916, 121982, 122089, 122142, 122214,
122254, 122640, 122893, 129695, 129815, 129952, 130285, 130387,
130627, 130792, 130880, 131346, 131388, 131468, 151617, 152110,
152471, 153071, 154769, 154835, 155339, 155472, 155686, 155806,
155873, 156059, 156133, 156226, 156287, 156327, 156395, 156768,
170859, 170939), Point.ID = 1:224), .Names = c("Stroke.ID..starttime.",
"sx", "sy", "time", "Point.ID"), row.names = c(NA, -224L), class = "data.frame")
答案 0 :(得分:1)
我认为你能做的最接近的事情如下。
如果df
是您的数据框:
df$coord <- mapply(function(x,y) c(x,y), df$sx, df$sy, SIMPLIFY = F)
输出结果为:
Stroke_ID_.starttime. sx sy time Point_ID coord
1 0 667.750 824.250 0 1 667.75, 824.25
2 0 668.500 824.500 53 2 668.5, 824.5
3 10412 231.250 602.250 10412 3 231.25, 602.25
4 10412 194.375 526.500 10852 4 194.375, 526.500
5 11726 226.125 603.500 11726 5 226.125, 603.500
6 11726 212.250 584.000 14766 6 212.25, 584.00
7 16355 422.125 665.625 16355 7 422.125, 665.625
8 16355 468.250 576.500 16635 8 468.25, 576.50
9 17103 406.750 647.625 17103 9 406.750, 647.625
10 17103 420.000 663.875 18089 10 420.000, 663.875
现在请记住,列坐标是一个数值向量列表。 为了证明这一点:
class(df$coord[[1]])
[1] "numeric"
因此,如果您想要访问第一个点的 sx ,则必须使用以下语法:
df$coord[[1]][1]
[1] 667.75
答案 1 :(得分:1)
您可以通过将点定义为以逗号(sx
)分隔的sy
和,
的串联来以不同方式处理它。例如,我们可以将Point1
定义为3, 4
;即(3,4)用数学术语表示。
您可以使用自己的data.frame来说明这一点。我已将其重命名为Points_df
。您可以创建Coordinates
列,将sx
值连接到以逗号分隔的sy
值。
Points_df$Coordinates <- apply(Points_df[,c("sx","sy")], 1, FUN=function(x) {
paste(x[1],x[2],sep=", ")
})
查看数据
Stroke.ID..starttime. sx sy time Point.ID Coordinates
0 667.75 824.25 0 1 667.75, 824.25
0 668.50 824.50 53 2 668.5, 824.5
现在创建一个辅助函数,它需要两个点并计算它们之间的距离。
distanceCalcultor <- function(Point1, Point2) {
Point1 <- as.numeric(unlist(strsplit(Point1,split=", ")))
Point2 <- as.numeric(unlist(strsplit(Point2,split=", ")))
distance <- sqrt((Point1[1]-Point2[1])^2+(Point1[2]-Point2[2])^2)
return(distance)
}
检查坐标1和5之间的距离
distanceCalcultor(Points_df$Coordinates[1],Points_df$Coordinates[5])
[1] 493.7238
我希望这能很好地补充。