I have a table like this
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",
)
These are "lat, long" values. I want to convert them into "long, lat". For example, i want to convert "78.1445111, 9.9365072" into "9.9365072,78.1445111". is it possible in r to automate for all columns ?
I am expecting an output like this in all the columns :
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"
)
what i tried, but failed :
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)
}
will this do? using dplyr
and tidyr
functions
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
used
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",
)