I have a dataframe that has a column of strings as follows:
mydata <- c("-1.356670,35.355030",
"-1.356670,35.355030",
"-1.356620,35.355890",
"-1.356930,35.358660",
"-1.357000,35.359060"
)
df <- data.frame(mydata)
I want to convert it into a dataframe containing two columns" long
and lat
, with each being a numeric type. What is the best way to do this? I've tried using lapply
, but cannot seem to make it work.
With base R you can do:
df$Long <- as.numeric(sapply(strsplit(as.character(df$mydata), ","), function(x) x[1]))
df$Lat <- as.numeric(sapply(strsplit(as.character(df$mydata), ","), function(x) x[2]))
mydata Long Lat
1 -1.356670,35.355030 -1.35667 35.35503
2 -1.356670,35.355030 -1.35667 35.35503
3 -1.356620,35.355890 -1.35662 35.35589
4 -1.356930,35.358660 -1.35693 35.35866
5 -1.357000,35.359060 -1.35700 35.35906
Or with tstrsplit()
from data.table
:
df$Long <- as.numeric(tstrsplit(df$mydata, ",")[[1]])
df$Lat <- as.numeric(tstrsplit(df$mydata, ",")[[2]])
Also with tstrsplit()
from data.table
as proposed by @clmarquart:
setDT(df)[, c("lat", "long") := tstrsplit(mydata, ",", fixed = TRUE)]
This can be done in one line in base R:
read.table(text = as.character(df$mydata), sep = ",", col.names = c("long", "lat"))
giving:
long lat
1 -1.35667 35.35503
2 -1.35667 35.35503
3 -1.35662 35.35589
4 -1.35693 35.35866
5 -1.35700 35.35906
A tidyverse
solution.
library(tidyverse)
dat <- df %>%
separate(mydata, into = c("Long", "Lat"), sep = ",", convert = TRUE)
# Print the data
dat
# Long Lat
# 1 -1.35667 35.35503
# 2 -1.35667 35.35503
# 3 -1.35662 35.35589
# 4 -1.35693 35.35866
# 5 -1.35700 35.35906
Using strsplit
with do.call
, then we just need assign the columns name
newdf=do.call(rbind.data.frame, strsplit(mydata,','))
names(newdf)=c('long','lat')
newdf
long lat
1 -1.356670 35.355030
2 -1.356670 35.355030
3 -1.356620 35.355890
4 -1.356930 35.358660
5 -1.357000 35.359060
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.