问题描述
我正在尝试在R
中编写一个函数,并使用pmap
函数进行调用,并使用从列表传递给{{的参数来重命名它创建的嵌套数据帧(或小标题)。 1}}函数。我认为最好用可重现的玩具示例来解释。这是一个(假设用户正在Windows中运行并且已经创建了目录C:\ temp \,并且当前为空,尽管您可以将以下路径设置为您选择的任何目录:
pmap
因此,当我运行#create some toy sample input data files
write.csv(x=data.frame(var1=c(42,43),var2=c(43,45)),file="C:\\temp\\AL.csv")
write.csv(x=data.frame(var1=c(22,file="C:\\temp\\AK.csv")
write.csv(x=data.frame(var1=c(90,98),var2=c(97,96)),file="C:\\temp\\AZ.csv")
write.csv(x=data.frame(var1=c(43,55),var2=c(85,43)),file="C:\\temp\\PossiblyUnkNownName.csv")
#Get list of files in c:\temp directory - assumes only files to be read in exist there
pathnames<-list.files(path = "C:\\temp\\",full.names=TRUE)
ListIdNumber<-c("ID3413241","ID3413242","ID3413243","ID3413244")
#Create a named list. In reality,my problem is more complex,but this gets at the root of the issue
mylistnames<-list(pathnames_in=pathnames,ListIdNumber_in=ListIdNumber)
#Functions that I've tried,where I'm passing the name ListIdNumber_in into the function so
#the resulting data frames are named.
#Attempt 1
get_data_files1<-function(pathnames_in,ListIdNumber_in){
tempdf <- read.csv(pathnames_in) %>% set_names(nm=ListIdNumber_in)
}
#Attempt 2
get_data_files2<-function(pathnames_in,ListIdNumber_in){
tempdf <- read.csv(pathnames_in)
names(tempdf)<-ListIdNumber_in
tempdf
}
#Attempt 3
get_data_files3<-function(pathnames_in,ListIdNumber_in){
tempdf <- read.csv(pathnames_in)
tempdf
}
#Fails
pmap(mylistnames,get_data_files1)->myoutput1
#Almost,but doesn't name the tibbles it creates and instead creates a variable named ListIdNumber_in
pmap(mylistnames,get_data_files2)->myoutput2
#This gets me the end result that I want,but I want to set the names inside the function
pmap(mylistnames,get_data_files3) %>% set_names(nm=mylistnames$ListIdNumber_in)->myoutput3
时,我想得到以下结果,只有我想在函数中完成嵌套数据帧/小对象的命名(而且我真的不需要我认为是错误创建的“ X”变量)。
pmap
有什么想法可以实现吗?
谢谢!
解决方法
- 在此处使用
map
- 无需创建命名列表,因为您无法在读取csv时在顶级附加名称,请分别添加名称。
library(purrr)
map(pathnames,read.csv) %>% set_names(ListIdNumber)
#$ID3413241
# var1 var2
#1 22 43
#2 43 45
#$ID3413242
# var1 var2
#1 42 43
#2 43 45
#$ID3413243
# var1 var2
#1 90 97
#2 98 96
#$ID3413244
# var1 var2
#1 43 85
#2 55 43
在基数R中,可以这样操作:
setNames(lapply(pathnames,read.csv),ListIdNumber)
之所以获得额外的X
列,是因为在编写csv时,您也在写行名。将其设置为row.names = FALSE
,您将没有该列。
write.csv(x=data.frame(var1=c(42,43),var2=c(43,45)),file="C:\\temp\\AL.csv",row.names = FALSE)
,
为此目的如何创建自己的pmap
?
# assume that your names are always stored in `ListIdNumber_in`
named_pmap <- function(.l,.f,...) set_names(pmap(.l,...),.l$ListIdNumber_in)
然后,您可以直接致电named_pmap(mylistnames,get_data_files3)
。除命名部分外,此named_pmap
与pmap
基本相同。