Question: I have a data frame like this,

```
DF1= c(
  "Name : John Miller, Math : 100, History : 80, Physics: 90",
  "Name : Mary Smith, French ： 99, History : 90, Physics: 89",
  "Name : Eddy Abbot, Math : 90, French : 85, Chemistry : 90"
)

```

Would like to make it a data.table in this way (better in data.table format)

```
          Name   Math   French History   Physics    Chemistry
1: John Miller    100       NA      80        90           NA
2:  Mary Smith     NA       99      90        89           NA
3:  Eddy Abbot     90       85      NA        NA           90

```

Wondering if my idea is at the right direction: 

1. Split the strings into words based on ",".
2. Get the keywords, "French, "Math", etc, based on " : ".
3. Fill in the right row and right col with the value respectively. and done.

Would like to invite advice on step 3 and many thanks.

Answer: 
Replace each comma and end-of-line with a newline and each space-colon with just colon. Read that using `readLines` to break up the strings into separate lines and use `trimws` to remove any junk whitespace. At this point the file is in Debian Control Format (DCF) so we can use `read.dcf` to read it creating character matrix `m`. Now convert `m` to data.table and convert the types.

```
dcf <- trimws(readLines(textConnection(gsub(" :", ":", gsub(",|$", "\n", DF1)))))
m <- read.dcf(textConnection(dcf))
DT <- as.data.table(m)[, lapply(.SD, type.convert, as.is = TRUE)]

```

giving:

```
> DT
          Name Math History Physics French Chemistry
1: John Miller  100      80      90     NA        NA
2:  Mary Smith   NA      90      89     99        NA
3:  Eddy Abbot   90      NA      NA     85        90

```

Note
----

We used the object name `DF1` for consistency with the question but it is a character vector, not a data frame, so you might want to choose a different name for it.
We convert it to a `tibble`,create a row names column ('rn'), expand the rows by splitting at `,` (`separate_rows`), `separate` the 'col' at `:` into 'col1' and 'col2', `spread` it to 'wide' format, and change the type 

```
library(tidyverse)
tibble(col = DF1) %>%
   rownames_to_column('rn') %>% 
   separate_rows(col, sep = "\\s*,\\s*")  %>% 
   separate(col, into = c('col1', 'col2'), sep="\\s*:\\s*") %>% 
   spread(col1, col2) %>%
   select(-rn) %>% 
   mutate_all(type.convert, as.is = TRUE) %>%
   select(Name, Math, French, History, Physics, Chemistry)
# A tibble: 3 x 6
#   Name         Math French History Physics Chemistry
#    <chr>       <int>  <int>   <int>   <int>     <int>
#1 John Miller   100     NA      80      90        NA
#2 Mary Smith     NA     99      90      89        NA
#3 Eddy Abbot     90     85      NA      NA        90

```

---

It is also possible to convert to `JSON` format and then use `fromJSON`

```
library(jsonlite)
out <-  fromJSON(paste0("[", paste("{", gsub('"(\\d+)"', "\\1", 
   gsub('(\\w+)\\s*:\\s*([^,]+)', '"\\1":"\\2"', DF1)), "}", sep="", collapse=",\n"), "]"))

out
#         Name Math History Physics French Chemistry
#1 John Miller  100      80      90     NA        NA
#2  Mary Smith   NA      90      89     99        NA
#3  Eddy Abbot   90      NA      NA     85        90

```