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:
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