Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataframe from a character vector where variable name and its data were stored jointly

I’ve this situation:

foo <- data.frame("vars" = c("animal: mouse | wks: 12 | site: cage | PI: 78",
                            "animal: dog | wks: 32 | GI: 0.2",
                            "animal: cat | wks: 8 | site: wild | PI: 13"))

where variable names and relative data were stored in character strings like the above example. In particular, each variable_name/its_data unit were delimited by a |. After the : there is the relative data.

I would like to have a final dataframe like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  animal  wks  site  PI   GI
  mouse   12   cage  78   NA
    dog   32   <NA>  NA  0.2
    cat    8   wild  13   NA

>Solution :

We may use read.dcf from base R

out <- type.convert(as.data.frame(read.dcf(
    textConnection(paste(gsub("\\s+\\|\\s+", "\n", foo$vars), 
    collapse="\n\n")))), as.is = TRUE)

-output

> out
  animal wks site PI  GI
1  mouse  12 cage 78  NA
2    dog  32 <NA> NA 0.2
3    cat   8 wild 13  NA
> str(out)
'data.frame':   3 obs. of  5 variables:
 $ animal: chr  "mouse" "dog" "cat"
 $ wks   : int  12 32 8
 $ site  : chr  "cage" NA "wild"
 $ PI    : int  78 NA 13
 $ GI    : num  NA 0.2 NA
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading