Home > front end >  R How to transform string with comma separate to data frame
R How to transform string with comma separate to data frame

Time:05-18

For Example:

string<-("15050505:20220513,19090909:20220515,19080808:20220513,20010101:20220515,23020202:20220515,23020402:20220515")

I want to transform this into Data Frame with Two Columns like this: enter image description here

CodePudding user response:

First identify your distinct rows by splitting on ,. This creates a list containing one vector with 6 elements, where each vector element is a row in your desired output.

s2 <- strsplit(string, ",")

[[1]]
[1] "15050505:20220513"
[2] "19090909:20220515"
[3] "19080808:20220513"
[4] "20010101:20220515"
[5] "23020202:20220515"
[6] "23020402:20220515"

Then split again on each :, which defines columns, and rbind() the result. This returns a matrix. Wrapping this in as.data.frame() returns (you guessed it) a dataframe.

as.data.frame(do.call(rbind, strsplit(s2[[1]], ":")))

        V1       V2
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515

CodePudding user response:

also

read.table(text=gsub(',', '\n', string), sep=":", col.names = c('ID', 'date'))

        ID     date
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515

Another compact way:

read.csv(text = c("ID,Date", chartr(":,", ",\n", string)))
        ID     Date
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515

CodePudding user response:

Another possible solution, based on tidyverse:

library(tidyverse)

string %>% 
  str_split(",", simplify = T) %>% t %>% as.data.frame %>% 
  separate(V1, into = c("Id", "Date"), sep = ":", convert = T) 

#>         Id     Date
#> 1 15050505 20220513
#> 2 19090909 20220515
#> 3 19080808 20220513
#> 4 20010101 20220515
#> 5 23020202 20220515
#> 6 23020402 20220515

CodePudding user response:

An alternative tidyverse approach:

library(tidyverse)
as_tibble(string) %>% 
  separate_rows(value, sep=",") %>% 
  separate(value, c("Id", "Date"))
  Id       Date    
  <chr>    <chr>   
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515
  • Related