簡體   English   中英

R:: 在 r 中將字符串從“x,y”反轉為“y,x”

[英]R:: reverse a string from “x,y” to “y,x” in r

我有一張這樣的桌子

aa<-tribble(
  ~"a",~"b",~"c",~"d",
" 78.1445111, 9.9365072",   "78.1444646, 9.9365044", " 78.1445111, 9.9365072",  "78.1444646, 9.9365044", 
"78.1444197, 9.9365166",    "78.1443816, 9.9365422",
"78.142359, 9.9365748", "78.1421918, 9.9366057",
"78.1421918, 9.9366057",    "78.1419488, 9.9367106",
"78.1444197, 9.9365166",    "78.1443816, 9.9365422",
"78.142359, 9.9365748", "78.1421918, 9.9366057",
"78.1421918, 9.9366057",    "78.1419488, 9.9367106",
  )

這些是“經緯度”值。 我想將它們轉換為“long, lat”。 例如,我想將“78.1445111, 9.9365072”轉換為“9.9365072,78.1445111”。 在 r 中是否可以對所有列進行自動化?

我期待在所有列中都有這樣的 output:

   a<- tribble(
      ~"a",~"b",~"c",~"d",
    "9.9365072,78.1445111", "9.9365044,78.1444646,", "9.9365072,78.1445111",    "9.9365044,78.1444646,", 
    "9.9365166,78.1444197", "9.9365422,78.1443816",
    "9.9365748,78.142359",  "9.9366057,78.1421918",
    "9.9366057,78.1421918", "9.9367106,78.1419488",
    "9.9365166,78.1444197", "9.9365422,78.1443816",
    "9.9365748,78.142359",  "9.9366057,78.1421918",
    "9.9366057,78.1421918", "9.9367106,78.1419488"
      )

我嘗試過,但失敗了:

bag_1<-list()
for(i in colnames(a)){
dummy <-str_split_fixed(a[[i]], ",", 2)

 bag_1[[i]]<-dummy
  
dum<-do.call(rbind,bag_1)
}

這會嗎? 使用dplyrtidyr功能

library(tidyverse)

aa %>% mutate(id = row_number()) %>% 
  pivot_longer(cols = -id) %>%
  separate(value, into = c("Lat", "Long"), sep = ", ") %>%
  mutate(new = paste(Long, Lat, sep = ", ")) %>%
  select(-Lat, -Long) %>%
  pivot_wider(id_cols = id, names_from = name, values_from = new)

# A tibble: 4 x 5
     id a                     b                     c                     d                    
  <int> <chr>                 <chr>                 <chr>                 <chr>                
1     1 9.9365072, 78.1445111 9.9365044, 78.1444646 9.9365072, 78.1445111 9.9365044, 78.1444646
2     2 9.9365166, 78.1444197 9.9365422, 78.1443816 9.9365748, 78.142359  9.9366057, 78.1421918
3     3 9.9366057, 78.1421918 9.9367106, 78.1419488 9.9365166, 78.1444197 9.9365422, 78.1443816
4     4 9.9365748, 78.142359  9.9366057, 78.1421918 9.9366057, 78.1421918 9.9367106, 78.1419488

用過的aa

aa <- tribble(
  ~"a",~"b",~"c",~"d",
  "78.1445111, 9.9365072",   "78.1444646, 9.9365044", "78.1445111, 9.9365072",  "78.1444646, 9.9365044", 
  "78.1444197, 9.9365166",    "78.1443816, 9.9365422",
  "78.142359, 9.9365748", "78.1421918, 9.9366057",
  "78.1421918, 9.9366057",    "78.1419488, 9.9367106",
  "78.1444197, 9.9365166",    "78.1443816, 9.9365422",
  "78.142359, 9.9365748", "78.1421918, 9.9366057",
  "78.1421918, 9.9366057",    "78.1419488, 9.9367106",
)

Base R ,您可以顛倒每列的strsplit()字符向量的順序:


sapply(aa, function(x){
  # first strsplit
  tmp1 <- strsplit(x, ", ", fixed = T)
                    
  # reverse and paste 
  sapply(tmp1, function(z){
    paste(rev(z), collapse = ", ")
  })
})

# yields

#      a                        b                       c                        d                      
# [1,] "9.9365072, 78.1445111" "9.9365044, 78.1444646" "9.9365072,  78.1445111" "9.9365044, 78.1444646"
# [2,] "9.9365166, 78.1444197"  "9.9365422, 78.1443816" "9.9365748, 78.142359"   "9.9366057, 78.1421918"
# [3,] "9.9366057, 78.1421918"  "9.9367106, 78.1419488" "9.9365166, 78.1444197"   "9.9365422, 78.1443816"
# [4,] "9.9365748, 78.142359"   "9.9366057, 78.1421918" "9.9366057, 78.1421918"  "9.9367106, 78.1419488"

您可以使用 dplyr::mutate 和 stringr::str_replace:

  # across all column
       aa %>% mutate(across(everything(),
  # capture the non-comma characters into 2 groups
              ~ str_replace((.),"([^,]+),([^,]+)",
  # switch first and second capture groups around
                "\\2,\\1"))) 

基本 R 選項:

寫一個 function 來反轉一個字符串:

reverse_string <- function(x) {
  trimws(paste(sub('.*,', '', x), sub(',.*', '', x), sep = ','))
}

使用lapply將其應用於每一列。

aa[] <- lapply(aa, reverse_string)
aa

# A tibble: 4 x 4
#    a                     b                    c                     d                   
#  <chr>                 <chr>                <chr>                 <chr>               
#1 9.9365072, 78.1445111 9.9365044,78.1444646 9.9365072, 78.1445111 9.9365044,78.1444646
#2 9.9365166,78.1444197  9.9365422,78.1443816 9.9365748,78.142359   9.9366057,78.1421918
#3 9.9366057,78.1421918  9.9367106,78.1419488 9.9365166,78.1444197  9.9365422,78.1443816
#4 9.9365748,78.142359   9.9366057,78.1421918 9.9366057,78.1421918  9.9367106,78.1419488

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM