重塑坐标列表(堆栈X在Y上)

时间:2017-09-09 04:48:53

标签: r stack reshape

我有一个数据框,其中包含一些坐标X,Y和一个类型。 数据框如下所示:

structure(list(Typ = c("Blue", "Blue", "Blue", 
"Blue", "Green", "Red", "Blue", "Blue", "Blue", 
"Red", "Red", "Red", "Cyan", "Cyan", "Cyan", "Blue", "Green", 
"Red", "Green", "Cyan", "Cyan", "Green", "Cyan", "Red", "Red", 
"Blue", "Red", "Blue", "Yellow", "Green"), X = c(429L, 
432L, 451L, 324L, 442L, 577L, 597L, 411L, 598L, 371L, 455L, 469L, 
380L, 437L, 316L, 387L, 414L, 482L, 506L, 478L, 496L, 574L, 395L, 
554L, 344L, 496L, 516L, 579L, 586L, 412L), Y = c(310L, 311L, 
311L, 312L, 312L, 312L, 312L, 314L, 314L, 315L, 315L, 315L, 316L, 
316L, 317L, 317L, 318L, 318L, 318L, 320L, 320L, 320L, 321L, 321L, 
322L, 322L, 322L, 322L, 323L, 324L)), .Names = c("Typ", "X", 
"Y"), row.names = c(NA, 30L), class = "data.frame")

并将获得如下输出。 另外,将Typ作为X坐标的颜色包含在内会很不错。

Y:   X:
310: 429
311: 432 451
312: 324 442 577 597
314: 411 598
315: 371 455 469
316: 380 437
317: 316 387
....

1 个答案:

答案 0 :(得分:1)

我们可以使用aggregate中的base R来创建list的' X'每个' Y'的值

aggregate(X~Y, df, I)
#    Y                  X
#1  310                429
#2  311           432, 451
#3  312 324, 442, 577, 597
#4  314           411, 598
#5  315      371, 455, 469
#6  316           380, 437
#7  317           316, 387
#8  318      414, 482, 506
#9  320      478, 496, 574
#10 321           395, 554
#11 322 344, 496, 516, 579
#12 323                586
#13 324                412