Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用name()重命名数据帧组的列_R_Dataframe_Lapply - Fatal编程技术网

R 使用name()重命名数据帧组的列

R 使用name()重命名数据帧组的列,r,dataframe,lapply,R,Dataframe,Lapply,我想用name函数重命名一组数据帧,但不能使用lappy或loop 我有一组数据帧名为qcew.2007、qcew.2014等。。。我有一个向量的名字,我希望所有的数据帧都有。他们都一样。向量名为colnm: colnm = c("area_fips" , "own_code", "industry_code", "agglvl_code") # example shortened # groups has names of all data frames and goes to 2013 g

我想用name函数重命名一组数据帧,但不能使用lappy或loop

我有一组数据帧名为qcew.2007、qcew.2014等。。。我有一个向量的名字,我希望所有的数据帧都有。他们都一样。向量名为colnm:

colnm = c("area_fips" , "own_code", "industry_code", "agglvl_code") # example shortened
 # groups has names of all data frames and goes to 2013
group =c("qcew.2007", "qcew.2008", "qcew.2009") 
    # using lapply
    names <- lapply(group, function(d){
     n = paste0(d)
     names(n) = colnm
    })
# using loop does not work either
   for (i in seq(group)) {
   names(group[[i]]) = colnm 
}   
colnm=c(“区域FIP”、“自有代码”、“行业代码”、“聚集代码”)#示例缩短
#组具有所有数据帧的名称,并转至2013年
组=c(“qcew.2007”、“qcew.2008”、“qcew.2009”)
#使用lappy

姓名给你。您需要使用
get
,否则将为
组中的字符向量指定名称

# sample data
qcew.2007 <- data.frame(a=1, b=2, c=3, d=4)
qcew.2008 <- data.frame(a=3, b=4, c=5, d=6)
qcew.2009 <- data.frame(a=5, b=6, c=7, d=8)    


for(i in 1:3)
    assign(group[i], `names<-`(get(group[i]), colnm))
names(qcew.2007)
# [1] "area_fips"     "own_code"      "industry_code" "agglvl_code"  
names(qcew.2008)
# [1] "area_fips"     "own_code"      "industry_code" "agglvl_code"  
names(qcew.2009)
# [1] "area_fips"     "own_code"      "industry_code" "agglvl_code"
#示例数据
qcew.2007同时:


你能告诉我为什么**
names@muraenok这是一种在一行中表达一切的方式。它与
x相同
 list2env(lapply(mget(group), setNames, colnm),envir=.GlobalEnv)

 names(qcew.2007)
 #[1] "area_fips"     "own_code"      "industry_code" "agglvl_code"