> 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
我想有一個包含單絃一柱全部源和目的地之間用逗號隔開的航點(lat lon) 我使用googleway包獲取航點
> 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 ...
讓我們保持簡單,使用汽車的簡單數據框我可以創建一個新的列,例如'cars $ test < - paste0(cars $ speed,「,」,cars $ dist)'。所以我認爲使用類似的東西會無用嗎? 'df $ route_str < - paste0(df $ route_line [[1]] [1],「,」,df $ route_line [[1]] [2]) 或者我錯過了解釋中的某些內容?如果是這樣,你能提供一些樣本數據,以便我們可以幫助你。 –
我編輯了這篇文章,解釋瞭如何獲取樣本數據。請檢查它。謝謝! :) –