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

Time:12-27

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

CodePudding user response:

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

CodePudding user response:

Here is a dplyr solution:

library(dplyr)
library(tidyr)

tibble(foo) %>%
  mutate(row = row_number()) %>% 
  separate_rows(vars, sep = '\\|') %>% 
  separate(vars, c("a", "b"), sep = '\\:') %>% 
  mutate(across(everything(), str_trim)) %>% 
  group_by(a) %>% 
  pivot_wider(names_from = a, values_from = b) %>% 
  type.convert(as.is = TRUE) %>% 
  select(-row)
  animal   wks site     PI    GI
  <chr>  <int> <chr> <int> <dbl>
1 mouse     12 cage     78  NA  
2 dog       32 NA       NA   0.2
3 cat        8 wild     13  NA 

CodePudding user response:

Another base R option using Reduce merge

type.convert(
  Reduce(
    function(x, y) merge(x, y, all = TRUE),
    lapply(
      strsplit(foo$vars, ":|\\|"),
      function(x) {
        m <- matrix(trimws(x), 2)
        setNames(data.frame(m[2, , drop = FALSE]), m[1, ])
      }
    )
  ),
  as.is = TRUE
)

gives

  animal wks site PI  GI
1    cat   8 wild 13  NA
2    dog  32 <NA> NA 0.2
3  mouse  12 cage 78  NA
  • Related