1

I am new to R, I am facing difficulties extracting data into new columns. My current data frame looks like this

          Column1.Column2.Column3
          10,22,32
          52,2,5
          51,29,6

And I wish to make it like this, in table format. How can i do it?

          Column1 Column2 Column3
          10      22      32
          52      2       5
          51      29      6
Jason Ching Yuk
  • 894
  • 2
  • 11
  • 30

2 Answers2

1

I assumed the first row was a header:

df <- read.table(text = "Column1.Column2.Column3
10,22,32
52,2,5
51,29,6", header = T)

df_new <- as.data.frame(do.call(rbind, str_split(df$Column1.Column2.Column3, ",")))
names(df_new) <- paste0("Column", 1:3)
thepule
  • 1,721
  • 1
  • 12
  • 22
1

We can use read.csv

read.csv(text=as.character(df[,1]), header=FALSE, col.names = scan(text=names(df),
                  what="", sep=".", quiet=TRUE))
#  Column1 Column2 Column3
#1      10      22      32
#2      52       2       5
#3      51      29       6
akrun
  • 874,273
  • 37
  • 540
  • 662