Converting a column string to numeric in an r data frame

末鹿安然 提交于 2021-01-27 15:09:12

问题


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.


回答1:


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)]



回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/54617973/converting-a-column-string-to-numeric-in-an-r-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!