R中的colnames()函数-将表值视为独立对象/变量 [英] colnames() function in R - Treating table values as independant objects/variables

查看:494
本文介绍了R中的colnames()函数-将表值视为独立对象/变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值列表,我希望将这些值用作从某个网站上的单独URL抓取的单独表的名称.

I have a list of values which I would like to use as names for separate tables scraped from separate URLs on a certain website.

> Fac_table
[[1]]
[1] "fulltime_fac_table"

[[2]]
[1] "parttime_fac_table"

[[3]]
[1] "honorary_fac_table"

[[4]]
[1] "retired_fac_table"

我想遍历列表以自动生成具有相应名称的4个表.

I would like to loop through the list to automatically generate 4 tables with the respective names.

结果应如下所示:

> fulltime_fac_table
    職稱          
V1  "教授兼系主任"
V2  "教授"        
V3  "教授"        
V4  "教授"        
V5  "特聘教授"    

> parttime_fac_table
    職稱       姓名    
V1  "教授"     "XXX"
V2  "教授"     "XXX"
V3  "教授"     "XXX"
V4  "教授"     "XXX"
V5  "教授"     "XXX"
V6  "教授"     "XXX"

我还有另一个名为标头"的列表,其中包含在线相应表的列标题.

I have another list, named 'headers', containing column headings of the respective tables online.

> headers
[[1]]
[1] "職稱"             "姓名"             "    研究領域"
[4] "聯絡方式"        

[[2]]
[1] "職稱"     "姓名"     "研究領域" "聯絡方式"

我可以使用以下代码为各个表分配值:

I was able to assign values to the respective tables with this code:

> assign(eval(parse(text="Fac_table[[i]]")), as_tibble(matrix(fac_data,
> nrow = length(headers[[i]])))

这将导致填充的表没有列标题,如下所示:

This results in a populated table, without column headings, like this one:

> honorary_fac_table
    [,1]       [,2]    
V1  "名譽教授" "XXX"
V2  "名譽教授" "XXX"
V3  "名譽教授" "XXX"
V4  "名譽教授" "XXX"

但是无法为每个表分配列名.

But was unable to assign column names to each table.

以下代码均无效:

> assign(colnames(eval(parse(text="Fac_table[1]"))), c(gsub("\\s", "", headers[[1]])))
Error in assign(colnames(eval(parse(text = "Fac_table[1]"))), c(gsub("\\s",  : 
  第一個引數不正確

> colnames(eval(parse(text="Fac_table[i]"))) <- c(gsub("\\s", "", headers[[i]]))
Error in colnames(eval(parse(text = "Fac_table[i]"))) <- c(gsub("\\s",  : 
  賦值目標擴充到非語言的物件

> do.call("<-", colnames(eval(parse(text="Fac_table[i]"))), c(gsub("\\s", "", headers[[i]])))
Error in do.call("<-", colnames(eval(parse(text = "Fac_table[i]"))), c(gsub("\\s",  : 
  second argument must be a list


为简化问题,以下是可重现的示例:


To simplify the issue, a reproducible example is as follows:

> varNamelist <- list(c("tbl1","tbl2","tbl3","tbl4"))
> colHeaderlist <- list(c("col1","col2","col3","col4"))
> tableData <- matrix([1:12], ncol=4)

这有效:

> assign(eval(parse(text="varNamelist[[1]][1]")), matrix(tableData, ncol
> = length(colHeaderlist[[1]])))

但这不是:

> colnames(as.name(varNamelist[[1]][1])) <- colHeaderlist[[1]]
Error in `colnames<-`(`*tmp*`, value = c("col1", "col2", "col3", "col4" : 
  attempt to set 'colnames' on an object with less than two dimensions


似乎R中的colnames()函数无法将"Fac_table [i]"表示的字符串当作变量名,其中可以存储独立数据(与Fac_table分开).


It seems like the colnames() function in R is unable to treat the strings as represented by "Fac_table[i]" as variable names, in which independent data (separate from Fac_table) can be stored.

> colnames(as.name(Fac_table[[1]])) <- headers[[1]]
Error in `colnames<-`(`*tmp*`, value = c("a", "b", "c",  : 
  attempt to set 'colnames' on an object with less than two dimensions

直接用'fulltime_fac_table'代替就可以了.

Substituting for 'fulltime_fac_table' directly works fine.

> colnames(fulltime_fac_table) <- headers[[1]]

有什么办法解决这个问题?

Is there any way around this issue?

谢谢!

推荐答案

有一个解决方案,但是我认为,如果我理解正确的话,当前设置可能会比必要的更为复杂.因此,我将尝试简化此任务.

There is a solution to this, but I think the current set up may be more complex than necessary if I understand correctly. So I'll try to make this task easier.

如果您要处理一维数据,我建议您使用向量,因为矢量比列表更适合用于此目的.因此,对于这个项目,我将从存储表和标题的名称开始,如下所示:

If you're working with one-dimensional data, I'd recommend using vectors, as they're more appropriate than lists for that purpose. So for this project, I'd begin by storing the names of tables and headers, like this:

varNamelist <- c("tbl1","tbl2","tbl3","tbl4")
colHeaderlist <- c("col1","col2","col3","col4")

从您的问题中确定这些表的输入的数据格式和来源仍然很困难,但是通常,只要不使用数据框,有时使用数据框比使用矩阵更容易大数据.这些步骤通常也不需要分配功能.相反,在设置数据框时,我们可以一次应用数据框的名称,列的名称和数据内容,如下所示:

It's still difficult to determine what the data format and origin for the input of these table is from your question, but in general, sometimes a data frame can be easier to work with than a matrix, as long as your not working with Big Data. The assign function is also typically not necessary for these sort of steps. Instead, when setting up a dataframe, we can apply the name of the data frame, the name of the columns, and the data contents all at once, like this:

tbl1 <- data.frame("col1"=c(1,2,3),
                   "col2"=c(4,5,6),
                   "col3"=c(7,8,9),
                   "col4"=c(10,11,12))

同样,我们使用由c()而不是list()表示的矢量来填充每一列,因为每一列都是它自己的一维.

Again, we're using vectors, noted by the c() instead of list(), to fill each column since each column is it's own single dimension.

要检查tbl1的输出,我们可以使用print():

To check the output of tbl1, we can then use print():

print(tbl1)

  col1 col2 col3 col4
1    1    4    7   10
2    2    5    8   11
3    3    6    9   12

如果可以选择更接近所示方式创建表,则可能会比使用那么多的列表和分配函数更容易.很快变得太复杂了.

If it's an option to create the tables closer to this way shown, that might make things easier than using so many lists and assign functions; that quickly becomes overly complicated.

但是,如果您想最后将所有表存储在一个位置,则可以将它们放在列表中:

But if you want at the end to store all the tables in a single place, you could put them in a list:

tableList <– list(tbl1=tbl1,tbl2=tbl2,tbl3=tbl3,tbl4=tbl4)

str(tableList)
List of 4
 $ tbl1:'data.frame':   3 obs. of  4 variables:
  ..$ col1: num [1:3] 1 2 3
  ..$ col2: num [1:3] 4 5 6
  ..$ col3: num [1:3] 7 8 9
  ..$ col4: num [1:3] 10 11 12
 $ tbl2:'data.frame':   3 obs. of  4 variables:
  ..$ col1: num [1:3] 1 2 3
  ..$ col2: num [1:3] 4 5 6
  ..$ col3: num [1:3] 7 8 9
  ..$ col4: num [1:3] 10 11 12
 $ tbl3:'data.frame':   3 obs. of  4 variables:
  ..$ col1: num [1:3] 1 2 3
  ..$ col2: num [1:3] 4 5 6
  ..$ col3: num [1:3] 7 8 9
  ..$ col4: num [1:3] 10 11 12
 $ tbl4:'data.frame':   3 obs. of  4 variables:
  ..$ col1: num [1:3] 1 2 3
  ..$ col2: num [1:3] 4 5 6
  ..$ col3: num [1:3] 7 8 9
  ..$ col4: num [1:3] 10 11 12

这篇关于R中的colnames()函数-将表值视为独立对象/变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆