我想獲得每個坐標,經緯度,但所有資料都在同一個單元格中。這個想法是將每個坐標放在不同的列中。
我創建了數字變數,但沒有獲得所有商店位置的數量。
liquor2 <- structure(list(`Store Location` = c(" -93.619455 42.022848",
" -93.669896 42.02160500000001", " -93.669896 42.02160500000001",
NA, NA, " -93.618911 42.022854", " -93.669896 42.02160500000001",
" -93.619455 42.022848", " -93.669896 42.02160500000001", NA,
" -93.669896 42.02160500000001", NA, " -93.618911 42.022854",
NA, " -93.618911 42.022854", NA, " -93.669896 42.02160500000001",
" -93.669896 42.02160500000001", " -93.610343 42.017115", " -93.618911 42.022854"
)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))
### Run this code but only obtained long data
liquor2 %>%
mutate(number= as.numeric(parse_number(`Store Location`)),
long=str_sub(number,1,10),
lat=str_sub(number, 11,13)) %>% View()
# `Store Location` number long lat
# <chr> <dbl> <chr> <chr>
# 1 " -93.619455 42.022848" -93.6 -93.619455 ""
# 2 " -93.669896 42.02160500000001" -93.7 -93.669896 ""
# 3 " -93.669896 42.02160500000001" -93.7 -93.669896 ""
# 4 NA NA NA NA
# 5 NA NA NA NA
# 6 " -93.618911 42.022854" -93.6 -93.618911 ""
# 7 " -93.669896 42.02160500000001" -93.7 -93.669896 ""
# 8 " -93.619455 42.022848" -93.6 -93.619455 ""
# 9 " -93.669896 42.02160500000001" -93.7 -93.669896 ""
# 10 NA NA NA NA
# 11 " -93.669896 42.02160500000001" -93.7 -93.669896 ""
# 12 NA NA NA NA
# 13 " -93.618911 42.022854" -93.6 -93.618911 ""
# 14 NA NA NA NA
# 15 " -93.618911 42.022854" -93.6 -93.618911 ""
# 16 NA NA NA NA
# 17 " -93.669896 42.02160500000001" -93.7 -93.669896 ""
# 18 " -93.669896 42.02160500000001" -93.7 -93.669896 ""
# 19 " -93.610343 42.017115" -93.6 -93.610343 ""
# 20 " -93.618911 42.022854" -93.6 -93.618911 ""
#
uj5u.com熱心網友回復:
使用tidyr::separate:
library(tidyverse)
liquor2 %>%
mutate(`Store Location` = trimws(`Store Location`)) %>%
separate(`Store Location`, sep = " ", into = c("Lat", "Lon")) %>%
mutate(across(Lat:Lon, as.numeric))
#> # A tibble: 20 x 2
#> Lat Lon
#> <dbl> <dbl>
#> 1 -93.6 42.0
#> 2 -93.7 42.0
#> 3 -93.7 42.0
#> 4 NA NA
#> 5 NA NA
#> 6 -93.6 42.0
#> 7 -93.7 42.0
#> 8 -93.6 42.0
#> 9 -93.7 42.0
#> 10 NA NA
#> 11 -93.7 42.0
#> 12 NA NA
#> 13 -93.6 42.0
#> 14 NA NA
#> 15 -93.6 42.0
#> 16 NA NA
#> 17 -93.7 42.0
#> 18 -93.7 42.0
#> 19 -93.6 42.0
#> 20 -93.6 42.0
編輯
要在輸出中查看更多小數點,我們可以更改列印的有效數字的數量:
options(pillar.sigfig = 8)
liquor2 %>%
mutate(`Store Location` = trimws(`Store Location`)) %>%
separate(`Store Location`, sep = " ", into = c("Lat", "Lon")) %>%
mutate(across(Lat:Lon, as.numeric))
#> # A tibble: 20 x 2
#> Lat Lon
#> <dbl> <dbl>
#> 1 -93.619455 42.022848
#> 2 -93.669896 42.021605
#> 3 -93.669896 42.021605
#> 4 NA NA
#> 5 NA NA
#> 6 -93.618911 42.022854
#> 7 -93.669896 42.021605
#> 8 -93.619455 42.022848
#> 9 -93.669896 42.021605
#> 10 NA NA
#> 11 -93.669896 42.021605
#> 12 NA NA
#> 13 -93.618911 42.022854
#> 14 NA NA
#> 15 -93.618911 42.022854
#> 16 NA NA
#> 17 -93.669896 42.021605
#> 18 -93.669896 42.021605
#> 19 -93.610343 42.017115
#> 20 -93.618911 42.022854
uj5u.com熱心網友回復:
使用base R
read.table(text = liquor2[[1]], header = FALSE,
col.names = c("Lat", "Lon"), strip.white = TRUE, fill = TRUE)
-輸出
Lat Lon
1 -93.61946 42.02285
2 -93.66990 42.02161
3 -93.66990 42.02161
4 NA NA
5 NA NA
6 -93.61891 42.02285
7 -93.66990 42.02161
8 -93.61946 42.02285
9 -93.66990 42.02161
10 NA NA
11 -93.66990 42.02161
12 NA NA
13 -93.61891 42.02285
14 NA NA
15 -93.61891 42.02285
16 NA NA
17 -93.66990 42.02161
18 -93.66990 42.02161
19 -93.61034 42.01711
20 -93.61891 42.02285
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525511.html
標籤:rtidyverse
