动态选择的名称列 data.frame
name columns of dynamically chosen data.frame
我正在尝试命名数据框的列,但数据框是动态选择的。知道为什么这不起作用吗?下面是一个例子,但在我的真实情况下,我得到了一个不同的错误。到目前为止,我只想知道是什么原因导致了这两个错误:
Error in file(filename, "r") : cannot open the connection
In addition: Warning message:
In file(filename, "r") :
cannot open file 'df': No such file or directory
#ASSIGN data frame name dynamically
> assign(as.character("df"), data.frame(c(1:10), c(11:20)))
>
#IT WOrked
> df
c.1.10. c.11.20.
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
>
#Call the data frame dynamically, it works
> eval(parse(text = c("df")))
c.1.10. c.11.20.
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
>
#name the columns
> colnames(df) <- c("a", "b")
> df
a b
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
>
#name columns of dynamically chosen data frame, returns and error
> colnames(eval(parse(text = c("df")))) <- c("c", "d")
Error in colnames(eval(parse(text = c("df")))) <- c("c", "d") :
target of assignment expands to non-language object
它不起作用,因为 R 不希望您将 assign
和(啊!)eval(parse())
用于此类基本内容。清单!这就是上帝创造清单的原因!
l <- list()
l[["df"]] <- data.frame(c(1:10), c(11:20))
colnames(l[["df"]]) <- c("a","b")
> l
$df
a b
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
我正在尝试命名数据框的列,但数据框是动态选择的。知道为什么这不起作用吗?下面是一个例子,但在我的真实情况下,我得到了一个不同的错误。到目前为止,我只想知道是什么原因导致了这两个错误:
Error in file(filename, "r") : cannot open the connection
In addition: Warning message:
In file(filename, "r") :
cannot open file 'df': No such file or directory
#ASSIGN data frame name dynamically
> assign(as.character("df"), data.frame(c(1:10), c(11:20)))
>
#IT WOrked
> df
c.1.10. c.11.20.
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
>
#Call the data frame dynamically, it works
> eval(parse(text = c("df")))
c.1.10. c.11.20.
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
>
#name the columns
> colnames(df) <- c("a", "b")
> df
a b
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
>
#name columns of dynamically chosen data frame, returns and error
> colnames(eval(parse(text = c("df")))) <- c("c", "d")
Error in colnames(eval(parse(text = c("df")))) <- c("c", "d") :
target of assignment expands to non-language object
它不起作用,因为 R 不希望您将 assign
和(啊!)eval(parse())
用于此类基本内容。清单!这就是上帝创造清单的原因!
l <- list()
l[["df"]] <- data.frame(c(1:10), c(11:20))
colnames(l[["df"]]) <- c("a","b")
> l
$df
a b
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20