Reputation: 23
How can I split this column up after exporting?
I need to get each value in a row separated by a space as a separate column.
Currently I have:
V1
X 1111 1324.01 1258.00 5154836
X 1111 1324.01 1258.00 5154836
X 1111 1324.01 1258.00 5154836
X 1111 1324.01 1258.00 5154836
And this is what I want:
V1 V2 V3 V4 V5
X 1111 1324.01 1258.00 5154836
X 1111 1324.01 1258.00 5154836
X 1111 1324.01 1258.00 5154836
X 1111 1324.01 1258.00 5154836
I tried:
separate(col = V1, into = c("V2", "V3", "V4", "V5", "V6"), sep = " ")
and:
SPS_X1 <- SPS_X %>% strsplit(SPS_X," ")
but these did not work for what I need.
P.S. my data frame has >5000000 rows
Upvotes: 0
Views: 367
Reputation: 39657
You can use read.table
to split a row into columns.
read.table(text=x$V1)
# V1 V2 V3 V4 V5
#1 X 1111 1324.01 1258 5154836
#2 X 1111 1324.01 1258 5154836
#3 X 1111 1324.01 1258 5154836
#4 X 1111 1324.01 1258 5154836
Data:
x <- data.frame(V1=c("X 1111 1324.01 1258.00 5154836","X 1111 1324.01 1258.00 5154836"
,"X 1111 1324.01 1258.00 5154836","X 1111 1324.01 1258.00 5154836"))
Upvotes: 0
Reputation: 483
base on your shared link made this script
file <- "DD_1.x"
clean_data <- function(string){
no_blank <- gsub("\\s+"," ", string) # Remove extra blank spaces
strsplit(no_blank," ")[[1]] # Split string by spaces, return array
}
l <- readLines(file, n = 200) # Read first 200 lines
data.frame(do.call("rbind",lapply(l,clean_data))) # Makes dataframe
Hope this can help you.
Remove extra blank spaces on R-project
Upvotes: 1