stats_nerd
stats_nerd

Reputation: 233

R- splitting dataframe column with string values (containing 2 numbers and separated by a comma) into 2 columns

I have a dataframe ddata where the variable Location has the Latitude and Longitude in string format separated by a comma. So when I type ddata$Location into my console I see this:

"33.9829, -118.3338"
"34.0454, -118.3157"
"33.942,  -118.2717"
"33.9572, -118.2717"

How do I separate this column by the comma, a delimiter, and get it to become 2 columns called: Longitude and Latitude? I have tried the split function but cannot get it to work.

Upvotes: 1

Views: 180

Answers (3)

Brian Syzdek
Brian Syzdek

Reputation: 948

library(tidyr) separate(ddata, ddata$Location, c("Longitude", "Latitude"), ",")

Upvotes: 1

Taher A. Ghaleb
Taher A. Ghaleb

Reputation: 5240

You can use the str_split_fixed function in the stringr package, like this:

library(stringr)
ddata[,c("Longitude", "Latitude")] <- str_split_fixed(ddata$Location, ", ", 2)

It will give you:

#             Location  Longitude   Latitude
# 1 33.9829, -118.3338    33.9829  -118.3338
# 2 34.0454, -118.3157    34.0454  -118.3157
# 3  33.942, -118.2717     33.942  -118.2717
# 4 33.9572, -118.2717    33.9572  -118.2717

Then, if you want to remove the Location column, you can just use this:

ddata$Location <- NULL

To get this:

#   Longitude   Latitude
# 1   33.9829  -118.3338
# 2   34.0454  -118.3157
# 3    33.942  -118.2717
# 4   33.9572  -118.2717

Hope it helps.

Upvotes: 2

akrun
akrun

Reputation: 886948

As it is separated by ,, the easiest option is read.csv (assuming that the class of 'Location' is character and not factor. If it is factor, convert it to character (with as.character(ddata$Location))

out <- read.csv(text = ddata$Location, header = FALSE,
      col.names = c("Latitude", "Longitude"))

Now, we cbind it with the original data

ddataNew <- cbind(dddata, out)

Upvotes: 3

Related Questions