I have a data file that looks like this:
ABC 123456789
DEF 987654321
GHI 123456789
I want to put this in a data table (in R) in this way:
ABC 1 2 3 4 5 6 7 8 9
DEF 9 8 7 6 5 4 3 2 1
GHI 1 2 3 4 5 6 7 8 9
So, the ABC together in one column, and the numbers each apart in separate columns.
I know how to split the data, and of course the numbers must be split by ''. But I don't know how to use this in this case, because I don't want to split the entire file by ''; only the second 'column', with the numbers.
Could anyone explain this to me?
We can use some regular expressions and read.table as follows. One can also use read.csv. The point is to introduce a comma separator and then use that to read in the data. See notes below:
res <- read.table(text=gsub("([0-9])()","\\1,","ABC 123456789
DEF 987654321
GHI 123456789"), sep = ",")[-10]
library(dplyr) # Just the pipe
res %>%
tidyr::separate(V1,c("ROW","FIRST"))
ROW FIRST V2 V3 V4 V5 V6 V7 V8 V9
1 ABC 1 2 3 4 5 6 7 8 9
2 DEF 9 8 7 6 5 4 3 2 1
3 GHI 1 2 3 4 5 6 7 8 9
Note
separate by using a much more general regex in our gsub.[-10] used above. It is possible that one may not know the length in which case str might be useful.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With