简体   繁体   中英

Extract names of a dataset with purrr::map2() or purrr::imap()

I am trying to use purrr::map2() or purrr::imap() to find a dataset from a large list of datasets where there is a given variable. Essentially, I will loop through the list of datasets and only print the names of the datasets that have the variable of interest. When I do it with purrr::map() , the dataset is unnamed ".x[[i]]". Any help would be greatly appreciated. Thank you

#load packages
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)

#create fictitious datasets
df1 <- tibble(score_a=1:20,
              sex_a=rep(c("M", "F"), 10))
df2 <- tibble(score_b=1:20,
              sex_b=rep(c("M", "F"), 10))
df3 <- tibble(score_c=1:20,
              sex_c=rep(c("M", "F"), 10))

#create a function that returns the dataset that
#contains a given variable
get_dataset_name <- function(data, contains){
  
  data_var_names <- colnames(data) 

  dataname <- deparse(substitute(data))
  
  if(contains %in% data_var_names){
    return(dataname)
  }
}

#testing the function
get_dataset_name(data=df3, contains="score_c")
#> [1] "df3"


#creating a list of the all datasets
data_list <- list(df1, df2, df3)

#looping through a list of the dataset to find the dataset
#that includes the given variable

map(data_list, get_dataset_name, contains="score_c")
#> [[1]]
#> NULL
#> 
#> [[2]]
#> NULL
#> 
#> [[3]]
#> [1] ".x[[i]]"

#I was hoping to obtain "df3" instead of  ".x[[i]]"
#I read that purrr::map2() or purrr::imap could solve
#the issue but I am not sure how to set it up
#Any help would be appreciated it



# map2(.x=data_list, 
#      .y=names(data_list), 
#      ~get_dataset_name(data=.x, contains="score_c"),
#      nest(.x, name=.y)
# )


#imap(data_list, get_dataset_name, contains="score_c")

Created on 2022-09-26 by the reprex package (v2.0.0)

Instead of deparse/substitute , try getting indices to the data set names. The indices can be logical or numeric. If the list is a named list then the names can be used to index the list and these values will be returned by the second function, iget_dataset_name .

# load packages
suppressPackageStartupMessages({
  library(dplyr)
  library(purrr)
})

# create fictitious datasets
df1 <- tibble(score_a=1:20,
              sex_a=rep(c("M", "F"), 10))
df2 <- tibble(score_b=1:20,
              sex_b=rep(c("M", "F"), 10))
df3 <- tibble(score_c=1:20,
              sex_c=rep(c("M", "F"), 10))

# create two functions that return indices to the
# data sets that contain the wanted name
get_dataset_name <- function(data, contains){
  data_var_names <- colnames(data) 
  contains %in% data_var_names
}
iget_dataset_name <- function(data, col_names, contains){
  data_var_names <- colnames(data) 
  i <- which(contains %in% data_var_names)
  col_names[i]
}

# testing the function
get_dataset_name(data=df3, contains="score_c")
#> [1] TRUE


# creating a list of the all datasets
data_list <- list(df1, df2, df3)

# looping through a list of the dataset to find the dataset
# that includes the given variable

map(data_list, get_dataset_name, contains="score_c")
#> [[1]]
#> [1] FALSE
#> 
#> [[2]]
#> [1] FALSE
#> 
#> [[3]]
#> [1] TRUE

imap(data_list, iget_dataset_name, contains="score_c")
#> [[1]]
#> integer(0)
#> 
#> [[2]]
#> integer(0)
#> 
#> [[3]]
#> [1] 3

# give the list a names attribute
names(data_list) <- c("df1", "df2", "df3")
# retest the functions
map(data_list, get_dataset_name, contains="score_c")
#> $df1
#> [1] FALSE
#> 
#> $df2
#> [1] FALSE
#> 
#> $df3
#> [1] TRUE

imap(data_list, iget_dataset_name, contains="score_c")
#> $df1
#> character(0)
#> 
#> $df2
#> character(0)
#> 
#> $df3
#> [1] "df3"

Created on 2022-09-27 with reprex v2.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM