我有这个示例数据:
df <- tibble(
"City1" = c("New York", "Boston", "Chicago"),
"City2" = c("Chicago", "Cleveland", "Atlanta"))
Run Code Online (Sandbox Code Playgroud)
假设City1
是起点,City2
也是目的地。即,一个人从纽约旅行到芝加哥。
我想为起始纬度添加一列,为起始经度添加一列,并对目的地城市也做同样的事情。总之,我想要四个新列。我已经有了坐标。
如何分配坐标?我试过使用case_when
,但我不确定如何将坐标传递到多列。做一列很容易:
library(tidyverse)
# The numbers after the cities are the latitudes
df <- df %>%
mutate(
City1_lat = case_when(
City1 == 'New York' ~ 40.7128,
City1 == 'Boston' ~ 42.3601,
City1 == 'Chicago' ~ 41.8781
)
)
Run Code Online (Sandbox Code Playgroud)
如何扩展它以添加到City1_lon
列中?试图尽可能地简化这一点,因为我有几千行起点/终点。adplyr
或base
解决方案都有效。我会将此扩展到目的地城市City2
。以供参考:
New York: 40.7128, 74.0060
Boston: 42.3601, 71.0589
Chicago: 41.8781, 87.6298
Cleveland: …
Run Code Online (Sandbox Code Playgroud) 我有一个创建一些新属性/内容的流程.我想从流文件中获取一个属性,并将其添加到文件的JSON内容中.
我可以使用AttributesToJSON
,但这只是覆盖文件内容.
我的流程示例如下:
在UpdateAttribute
该属性列表添加当前时间,然后AttributestoJSON
添加到实际的JSON的属性,但它也覆盖了整个流程文件的内容.我试图解决这个问题MergeRecord
,但我不能将这两个文件合并回来(使用CurrentTime
相关属性名称).
有没有比这更好的了?在MergeRecord
刚刚想出了一个错误.(也不知道如何配置RecordReader/Writer,因为我想保留JSON中的所有内容).
我正在从以下 JSON/AVRO 模式生成随机数据:
{
"type" : "record",
"namespace" : "test",
"name" : "metro_data",
"fields": [
{
"name" : "PersonID",
"type" : "int"
},
{
"name" : "TripStartStation",
"type" : {
"type" : "enum",
"name" : "StartStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO"]
}
},
{
"name" : "TripEndStation",
"type" : {
"type" : "enum",
"name" : "EndStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO""]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
上面的模式生成这个,例如:
[ {
"PersonID" : -1089196095,
"TripStartStation" : "WIEHLE_RESTON_EAST",
"TripEndStation" : "SPRING_HILL"
}
Run Code Online (Sandbox Code Playgroud)
我想获取PersonID …