如何连接作为数据框列表R的列中的所有值
How to concatenate all vlaues in a column which is a list of data frames R
数据框的结构
> str(df)
'data.frame': 459 obs. of 6 variables:
$ Source : chr "Mumbai" "Mumbai" "Bangalore" "Bangalore" ...
$ Destination: chr "Bangalore" "Bangalore" "Chennai" "Cochin" ...
$ src_loc :'data.frame': 459 obs. of 2 variables:
..$ lon: num 72.9 72.9 77.6 77.6 73.9 ...
..$ lat: num 19.1 19.1 13 13 18.5 ...
$ dest_loc :'data.frame': 459 obs. of 2 variables:
..$ lon: num 77.6 77.6 80.3 76.3 78.5 ...
..$ lat: num 12.97 12.97 13.08 9.93 17.39 ...
$ route_line:List of 459
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 244 obs. of 2 variables:
.. ..$ lat: num 13 13 13 13 13 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.6 ...
..$ :'data.frame': 228 obs. of 2 variables:
.. ..$ lat: num 13 13 13 12.9 12.9 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.6 ...
..$ :'data.frame': 232 obs. of 2 variables:
.. ..$ lat: num 18.5 18.5 18.5 18.5 18.5 ...
.. ..$ lon: num 73.9 73.9 73.9 73.9 73.9 ...
..$ :'data.frame': 234 obs. of 2 variables:
.. ..$ lat: num 15.4 15.4 15.4 15.4 15.4 ...
.. ..$ lon: num 75.1 75.1 75.1 75.1 75.1 ...
..$ :'data.frame': 218 obs. of 2 variables:
.. ..$ lat: num 17.4 17.4 17.4 17.5 17.5 ...
.. ..$ lon: num 78.5 78.5 78.5 78.5 78.5 ...
等等..
> df$route_line[[1]] #gives a data frame
lat lon
1 19.07597 72.87765
2 19.06575 72.89918
3 19.06331 72.91443
4 19.05159 72.93661
5 19.06758 72.98437
6 19.06653 73.02000
7 19.04099 73.02868
8 19.02309 73.04452
9 19.03844 73.07676
10 18.99688 73.13215
11 18.98191 73.14718
12 18.96049 73.15789
13 18.94201 73.15694
14 18.92484 73.16662
15 18.89439 73.20433
16 18.84075 73.24026
17 18.81434 73.27669
18 18.79409 73.29148
19 18.77373 73.32182
20 18.77023 73.33760
21 18.76414 73.34698
22 18.77114 73.36076
23 18.76580 73.35765
24 18.77090 73.36348
25 18.75822 73.37283
26 18.76368 73.38653
27 18.76939 73.40145
28 18.76301 73.41848
29 18.75766 73.42920
30 18.73973 73.42921
我想创建一个新列(名称为route_str),其中包含通过将上面获得的数据框中的所有纬度和经度连接起来获得的字符串 df
中的每一行
例如,
> df$route_str[1] #should give
[1] "19.07597 72.87765, 19.06575 72.89918, 19.06331 72.91443,19.05159 72.93661..." so on till 30
我试过了
> fun <- function(ip)
+ {
+ a <- ip[[1]]
+ a[3] <- paste(a[1],a[2]," ")
+ op <- paste(a[3],collapse = ",")
+ return(op)
+ }
> df$route_str <- lapply(df$route_line,fun)
但我得到的输出是
> unique_routes$route_str[1]
[[1]]
[1] "19.0759696960449 19.0657501220703 "
我尝试使用以下代码创建可重现的数据,但结构不一样
df <- data.frame(src=c("chennai","Mumbai","Bangalore"),dest=c("Mumbai","Bangalore","Mumbai"),route=list(list(lat=c(19,20,21),lon=c(72,73,74)),data.frame(lat=c(19,20,21),lon=c(72,73,74)),data.frame(lat=c(19,20,21),lon=c(72,73,74))))
但是上面创建的数据结构如下
> str(df)
'data.frame': 3 obs. of 8 variables:
$ src : Factor w/ 3 levels "Bangalore","chennai",..: 2 3 1
$ dest : Factor w/ 2 levels "Bangalore","Mumbai": 2 1 2
$ route.lat : num 19 20 21
$ route.lon : num 72 73 74
$ route.lat.1: num 19 20 21
$ route.lon.1: num 72 73 74
$ route.lat.2: num 19 20 21
$ route.lon.2: num 72 73 74
我在 windows 10 上使用 R 版本 3.3.1,请帮忙!
编辑:
这就是我最终得到那个复杂数据框的方式
初始数据框是这样的
> df <- data.frame(source=c("chennai","Mumbai","Bangalore"),destination=c("Mumbai","Bangalore","Mumbai"))
> df
source destination
1 chennai Mumbai
2 Mumbai Bangalore
3 Bangalore Mumbai
我想要一个包含单个字符串的列,其中源和目的地之间的所有路径点(经纬度)用逗号分隔
我使用 googleway 包来获取 waypoints
> library(googleway)
> res <- function(src,dest,key) #key is google maps API key
+ {
+ polylinex <- google_directions(origin = src,destination = dest,key = key)
+ return(polylinex$routes$overview_polyline$points)
+ }
> df$source <- as.character(df$source)
> df$destination <- as.character(df$destination)
> df$x <- mapply(res,df$source,df$destination,key)
> df$route_line <- lapply(df$x,function(y) googleway::decode_pl(y))
> df <- df[,!(names(df)=="x")]
> str(df)
'data.frame': 3 obs. of 3 variables:
$ source : chr "chennai" "Mumbai" "Bangalore"
$ destination: chr "Mumbai" "Bangalore" "Mumbai"
$ route_line :List of 3
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 13.1 13.1 13.1 13.1 13.1 ...
.. ..$ lon: num 80.3 80.2 80.2 80.2 80.2 ...
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 218 obs. of 2 variables:
.. ..$ lat: num 13 13 13 13 13 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.5 ...
将您的 lapply
略微修改为 sapply
,并稍微改变 paste
序列将得到您想要的
df$route_str <- sapply(df$x, function(y){
df_coords <- decode_pl(y)
paste0(t(sapply(df_coords, paste0)), collapse = ",")
})
str(df)
'data.frame': 3 obs. of 4 variables:
$ source : chr "chennai" "Mumbai" "Bangalore"
$ destination: chr "Mumbai" "Bangalore" "Mumbai"
$ x : chr "weznA{z|hNjrAlkDue@vsDtVnhD|dAnkErSbdI~kGzmRtmLjrNldI|iWnjBbuDf^duJgPzqNsiCtaIyLpnOyXzrKe{AvaG|JxpF~VpkCga@tkG_sBp|Cev@fvDpI|gF"| __truncated__ "ywlsBi|x{Lz~@qeCfNi~AfhAsiC}bBoiHpEu}Er~Cgu@znB_bB}~AohEvbGeyIp|A}|AzdC}aAnrB|DhjBo{@h}DujFfnIq_F`dDubFp}Bm{Af~Bs|DzTsaB`e@uy@w"| __truncated__ "oodnA}drxMkcAhKggApm@s}A|uAey@|rAi~BdjF{fDpaLgxB||F}`DvxE{sDdmDgkGthKmlK|vJmgIbzJa`BrjCssC|aBw`Dvw@osBrkCutNpbIigD|sCk`Ft_C}iPv"| __truncated__
$ route_str : chr "13.0826797485352,80.2706985473633,13.0693397521973,80.2431106567383,13.0755300521851,80.2141876220703,13.0717391967773,80.18706"| __truncated__ "19.0759696960449,72.8776473999023,19.0657501220703,72.8991775512695,19.0633087158203,72.9144287109375,19.0515899658203,72.93660"| __truncated__ "12.9715995788574,77.5945510864258,12.9825401306152,77.5925750732422,12.9940996170044,77.5851287841797,13.0092391967773,77.57122"| __truncated__
注意:我是 googleway
作者,感谢您使用该软件包
数据框的结构
> str(df)
'data.frame': 459 obs. of 6 variables:
$ Source : chr "Mumbai" "Mumbai" "Bangalore" "Bangalore" ...
$ Destination: chr "Bangalore" "Bangalore" "Chennai" "Cochin" ...
$ src_loc :'data.frame': 459 obs. of 2 variables:
..$ lon: num 72.9 72.9 77.6 77.6 73.9 ...
..$ lat: num 19.1 19.1 13 13 18.5 ...
$ dest_loc :'data.frame': 459 obs. of 2 variables:
..$ lon: num 77.6 77.6 80.3 76.3 78.5 ...
..$ lat: num 12.97 12.97 13.08 9.93 17.39 ...
$ route_line:List of 459
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 244 obs. of 2 variables:
.. ..$ lat: num 13 13 13 13 13 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.6 ...
..$ :'data.frame': 228 obs. of 2 variables:
.. ..$ lat: num 13 13 13 12.9 12.9 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.6 ...
..$ :'data.frame': 232 obs. of 2 variables:
.. ..$ lat: num 18.5 18.5 18.5 18.5 18.5 ...
.. ..$ lon: num 73.9 73.9 73.9 73.9 73.9 ...
..$ :'data.frame': 234 obs. of 2 variables:
.. ..$ lat: num 15.4 15.4 15.4 15.4 15.4 ...
.. ..$ lon: num 75.1 75.1 75.1 75.1 75.1 ...
..$ :'data.frame': 218 obs. of 2 variables:
.. ..$ lat: num 17.4 17.4 17.4 17.5 17.5 ...
.. ..$ lon: num 78.5 78.5 78.5 78.5 78.5 ...
等等..
> df$route_line[[1]] #gives a data frame
lat lon
1 19.07597 72.87765
2 19.06575 72.89918
3 19.06331 72.91443
4 19.05159 72.93661
5 19.06758 72.98437
6 19.06653 73.02000
7 19.04099 73.02868
8 19.02309 73.04452
9 19.03844 73.07676
10 18.99688 73.13215
11 18.98191 73.14718
12 18.96049 73.15789
13 18.94201 73.15694
14 18.92484 73.16662
15 18.89439 73.20433
16 18.84075 73.24026
17 18.81434 73.27669
18 18.79409 73.29148
19 18.77373 73.32182
20 18.77023 73.33760
21 18.76414 73.34698
22 18.77114 73.36076
23 18.76580 73.35765
24 18.77090 73.36348
25 18.75822 73.37283
26 18.76368 73.38653
27 18.76939 73.40145
28 18.76301 73.41848
29 18.75766 73.42920
30 18.73973 73.42921
我想创建一个新列(名称为route_str),其中包含通过将上面获得的数据框中的所有纬度和经度连接起来获得的字符串 df
中的每一行例如,
> df$route_str[1] #should give
[1] "19.07597 72.87765, 19.06575 72.89918, 19.06331 72.91443,19.05159 72.93661..." so on till 30
我试过了
> fun <- function(ip)
+ {
+ a <- ip[[1]]
+ a[3] <- paste(a[1],a[2]," ")
+ op <- paste(a[3],collapse = ",")
+ return(op)
+ }
> df$route_str <- lapply(df$route_line,fun)
但我得到的输出是
> unique_routes$route_str[1]
[[1]]
[1] "19.0759696960449 19.0657501220703 "
我尝试使用以下代码创建可重现的数据,但结构不一样
df <- data.frame(src=c("chennai","Mumbai","Bangalore"),dest=c("Mumbai","Bangalore","Mumbai"),route=list(list(lat=c(19,20,21),lon=c(72,73,74)),data.frame(lat=c(19,20,21),lon=c(72,73,74)),data.frame(lat=c(19,20,21),lon=c(72,73,74))))
但是上面创建的数据结构如下
> str(df)
'data.frame': 3 obs. of 8 variables:
$ src : Factor w/ 3 levels "Bangalore","chennai",..: 2 3 1
$ dest : Factor w/ 2 levels "Bangalore","Mumbai": 2 1 2
$ route.lat : num 19 20 21
$ route.lon : num 72 73 74
$ route.lat.1: num 19 20 21
$ route.lon.1: num 72 73 74
$ route.lat.2: num 19 20 21
$ route.lon.2: num 72 73 74
我在 windows 10 上使用 R 版本 3.3.1,请帮忙!
编辑:
这就是我最终得到那个复杂数据框的方式
初始数据框是这样的
> df <- data.frame(source=c("chennai","Mumbai","Bangalore"),destination=c("Mumbai","Bangalore","Mumbai"))
> df
source destination
1 chennai Mumbai
2 Mumbai Bangalore
3 Bangalore Mumbai
我想要一个包含单个字符串的列,其中源和目的地之间的所有路径点(经纬度)用逗号分隔 我使用 googleway 包来获取 waypoints
> library(googleway)
> res <- function(src,dest,key) #key is google maps API key
+ {
+ polylinex <- google_directions(origin = src,destination = dest,key = key)
+ return(polylinex$routes$overview_polyline$points)
+ }
> df$source <- as.character(df$source)
> df$destination <- as.character(df$destination)
> df$x <- mapply(res,df$source,df$destination,key)
> df$route_line <- lapply(df$x,function(y) googleway::decode_pl(y))
> df <- df[,!(names(df)=="x")]
> str(df)
'data.frame': 3 obs. of 3 variables:
$ source : chr "chennai" "Mumbai" "Bangalore"
$ destination: chr "Mumbai" "Bangalore" "Mumbai"
$ route_line :List of 3
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 13.1 13.1 13.1 13.1 13.1 ...
.. ..$ lon: num 80.3 80.2 80.2 80.2 80.2 ...
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 218 obs. of 2 variables:
.. ..$ lat: num 13 13 13 13 13 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.5 ...
将您的 lapply
略微修改为 sapply
,并稍微改变 paste
序列将得到您想要的
df$route_str <- sapply(df$x, function(y){
df_coords <- decode_pl(y)
paste0(t(sapply(df_coords, paste0)), collapse = ",")
})
str(df)
'data.frame': 3 obs. of 4 variables:
$ source : chr "chennai" "Mumbai" "Bangalore"
$ destination: chr "Mumbai" "Bangalore" "Mumbai"
$ x : chr "weznA{z|hNjrAlkDue@vsDtVnhD|dAnkErSbdI~kGzmRtmLjrNldI|iWnjBbuDf^duJgPzqNsiCtaIyLpnOyXzrKe{AvaG|JxpF~VpkCga@tkG_sBp|Cev@fvDpI|gF"| __truncated__ "ywlsBi|x{Lz~@qeCfNi~AfhAsiC}bBoiHpEu}Er~Cgu@znB_bB}~AohEvbGeyIp|A}|AzdC}aAnrB|DhjBo{@h}DujFfnIq_F`dDubFp}Bm{Af~Bs|DzTsaB`e@uy@w"| __truncated__ "oodnA}drxMkcAhKggApm@s}A|uAey@|rAi~BdjF{fDpaLgxB||F}`DvxE{sDdmDgkGthKmlK|vJmgIbzJa`BrjCssC|aBw`Dvw@osBrkCutNpbIigD|sCk`Ft_C}iPv"| __truncated__
$ route_str : chr "13.0826797485352,80.2706985473633,13.0693397521973,80.2431106567383,13.0755300521851,80.2141876220703,13.0717391967773,80.18706"| __truncated__ "19.0759696960449,72.8776473999023,19.0657501220703,72.8991775512695,19.0633087158203,72.9144287109375,19.0515899658203,72.93660"| __truncated__ "12.9715995788574,77.5945510864258,12.9825401306152,77.5925750732422,12.9940996170044,77.5851287841797,13.0092391967773,77.57122"| __truncated__
注意:我是 googleway
作者,感谢您使用该软件包