Home > OS >  Trouble turning a text file with a character string into a column with one character per row in R
Trouble turning a text file with a character string into a column with one character per row in R

Time:12-03

I am relatively new to R and am attempting to turn a text file with a long character string into a single column of a data table with one character per row. I have tried reading in the text file using read_file from the readr package and then making the character string a list and finally using cbind, as below:

df <- data.frame("id" = 1:9)
string <-read_file("StringFilePath.txt")
string <- as.list(string)
df <- cbind(df, string)

Unfortunately, I end up with all of the characters in the string in the first row of the data frame. I have also attempted, perhaps misguidedly, to add a comma between each character and then create a .csv file that I could more easily work with as below:

string <- gsub("(?<=.)(?=.)", ",", string, perl = TRUE)
write.csv(string, "StringFilePath.csv",
          row.names = FALSE)

However, the .csv ended up with roughly half of the characters in the first row for reasons that escape me. Any suggestions for a solution to this seemingly simple problem would be greatly appreciated!

CodePudding user response:

You can use strsplit. Just be aware that strsplit returns a list and hence you have to convert it into a character vector

string <- "jasdklnjabfial"
data.table::data.table(x = unlist(strsplit(string, "")))
#>     x
#>  1: j
#>  2: a
#>  3: s
#>  4: d
#>  5: k
#>  6: l
#>  7: n
#>  8: j
#>  9: a
#> 10: b
#> 11: f
#> 12: i
#> 13: a
#> 14: l
  • Related