user113156
user113156

Reputation: 7107

Extract specific cells from a data frame and flatten the data into a single row

I have some data which looks like:

             Variable_Nombre_1 Variable_Codigo_1                     Variable_Nombre_2 Variable_Codigo_2
1                    Dato base              <NA>                          Tipo de dato              <NA>
2         Alella sección 01004        0800301004                             Secciones              SECC
3 Fuente de ingreso: pensiones              <NA> Distribución de la fuente de ingresos              <NA>

I would like to extract specific cells of the data.

Cells to extract:

x[2, 1]
x[3, 1]
x[2, 2]
x[2, 3]
x[3, 3]

Then flatten the data using pivot_wider of the extracted cells. I am trying to do this using the pipe operator since I have many data frames in a list that I would like to map over and extract these cells, then flatten the data.

Data:

d <- structure(list(Variable_Nombre_1 = c("Dato base", "Alella sección 01004", 
"Fuente de ingreso: pensiones"), Variable_Codigo_1 = c(NA, "0800301004", 
NA), Variable_Nombre_2 = c("Tipo de dato", "Secciones", "Distribución de la fuente de ingresos"
), Variable_Codigo_2 = c(NA, "SECC", NA)), class = "data.frame", row.names = c(NA, 
-3L))

Upvotes: 1

Views: 99

Answers (1)

akrun
akrun

Reputation: 887213

If we want to select specific cells, then use cbind on the row/column vector index to extract as a vector

v1 <- d[cbind(c(2, 3, 2, 2, 3), c(1, 1, 2, 3, 3))]

Then, we can use as.data.frame.list to convert the vector to a single row

as.data.frame.list(v1)

Or name the vector and then use as_tibble_row

library(tibble)
names(v1) <- paste0("V", seq_along(v1))
as_tibble_row(v1)
# A tibble: 1 x 5
  V1                   V2                           V3         V4        V5                                   
  <chr>                <chr>                        <chr>      <chr>     <chr>                                
1 Alella sección 01004 Fuente de ingreso: pensiones 0800301004 Secciones Distribución de la fuente de ingresos

If this needs to be done in a list, then loop over the list with map and apply the same steps

library(purrr)
library(dplyr)
library(stringr)
map(lst1, ~ {
     v1 <- .x[cbind(c(2, 3, 2, 2, 3), c(1, 1, 2, 3, 3))]
     names(v1) <- str_c("V", seq_along(v1))
     as_tibble_row(v1)
})

Upvotes: 1

Related Questions