Home > database >  Create as many columns in a data frame as white spaces are in a string r
Create as many columns in a data frame as white spaces are in a string r

Time:03-17

I have data separated by a white space:

question <- c("ENERO 602 1372 464 1505 S/C 610 1358 244 1424 S/C S/D S/D 1868 S/C",
          "FEBRERO 646 1526 504 1620 S/C 610 1443 251 1525 S/C S/D S/D 1873 S/C")

Data frame:

df <- data.frame(question)

The results are the following:

> data.frame(question)
                                                              question
1   ENERO 602 1372 464 1505 S/C 610 1358 244 1424 S/C S/D S/D 1868 S/C
2 FEBRERO 646 1526 504 1620 S/C 610 1443 251 1525 S/C S/D S/D 1873 S/C

There is only one column, do you know how to separate them? Help is much appreciated.

CodePudding user response:

With space character, it is easier to read with read.table from base R and this will automatically assign the type of the columns based on its value

read.table(text = question, header = FALSE)

CodePudding user response:

If this is already vector as you have suggested above, the below will seperate into separate strings.

question %>% str_split(" ")
  •  Tags:  
  • r
  • Related