Greetings,
I have this data frame which is a result from reading a PDF file with tabulizer::extract_tables()
library(dplyr)
library(tidyr)
dat <- structure(list(Name = c("Product 1", "Product 2", "Product 3",
"Product 4", "Product 5", "Product 6"), Size = c(750, 1750, 1750,
50, 375, 375), PK = c("1 19.95", "6 36.95", "6", "12", "1",
"1 13.95"), Metric = c(NA, NA, 69.95, 1, 13.95, NA)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L), class = "data.frame")
dat
#> Name Size PK Metric
#> 1 Product 1 750 1 19.95 NA
#> 2 Product 2 1750 6 36.95 NA
#> 3 Product 3 1750 6 69.95
#> 4 Product 4 50 12 1.00
#> 5 Product 5 375 1 13.95
#> 6 Product 6 375 1 13.95 NA
str(dat)
#> 'data.frame': 6 obs. of 4 variables:
#> $ Name : chr "Product 1" "Product 2" "Product 3" "Product 4" ...
#> $ Size : num 750 1750 1750 50 375 375
#> $ PK : chr "1 19.95" "6 36.95" "6" "12" ...
#> $ Metric: num NA NA 70 1 13.9 ...
I want to separate the PK
column then merge with the Metric
column (& remove all NA
s) to get this final table
#> Name Size PK Metric
#> 1 Product 1 750 1 19.95
#> 2 Product 2 1750 6 36.95
#> 3 Product 3 1750 6 69.95
#> 4 Product 4 50 12 1.00
#> 5 Product 5 375 1 13.95
#> 6 Product 6 375 1 13.95
I tried tidyr::separate()
first but the values that I want to merge with Metric
column went away
dat_sep <- dat %>%
separate(PK, into = c("c1", "c2"), sep = " ",
convert = TRUE, remove = FALSE)
#> Warning: Expected 2 pieces. Additional pieces discarded in 3 rows [1, 2,
#> 6].
#> Warning: Expected 2 pieces. Missing pieces filled with `NA` in 3 rows [3,
#> 4, 5].
dat_sep
#> Name Size PK c1 c2 Metric
#> 1 Product 1 750 1 19.95 1 NA NA
#> 2 Product 2 1750 6 36.95 6 NA NA
#> 3 Product 3 1750 6 6 NA 69.95
#> 4 Product 4 50 12 12 NA 1.00
#> 5 Product 5 375 1 1 NA 13.95
#> 6 Product 6 375 1 13.95 1 NA NA
How do I go about fixing this?
Thank you!