Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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
没有列名的data.frames的奇怪行为_R_Dataframe - Fatal编程技术网

没有列名的data.frames的奇怪行为

没有列名的data.frames的奇怪行为,r,dataframe,R,Dataframe,没有列名的data.frames出现意外行为。以下工作如预期: df <- data.frame(a = 1:5, b = 5:9) df + 1 ## a b ## 1 2 6 ## 2 3 7 ## 3 4 8 df在data.frame的文档中,它说: 列名应为非空,尝试使用空名称将产生不受支持的结果 因此,如果列名为空,则预期结果可能不是期望的结果 我认为这最终是因为R将data.frame对象视为具有特定属性的列表: ## A list with no attrib

没有列名的data.frames出现意外行为。以下工作如预期:

df <- data.frame(a = 1:5, b = 5:9)
df + 1
##   a  b
## 1 2  6
## 2 3  7
## 3 4  8

df在
data.frame
的文档中,它说:

列名应为非空,尝试使用空名称将产生不受支持的结果


因此,如果列名为空,则预期结果可能不是期望的结果

我认为这最终是因为R将
data.frame
对象视为具有特定属性的列表:

## A list with no attributes
list_no_attr1 <- list(c(1,2,3), c(3,2,1))

## The attributes and class of the list
attributes(list_no_attr1)
#> NULL
class(list_no_attr1)
#> "list"
这仍然是一个列表。现在,当我们将对象的类更改为
“data.frame”
时,它将对
数据.frame
使用S3方法,对
打印
和所有其他相关函数使用S3方法

## Adding a data.frame class attribute
list_data_frame <- list3
attr(list_data_frame, "class") <- "data.frame"

## The attributes and class of the list
attributes(list_data_frame)
#> $names
#> [1] "A" "B"
#> $row.names
#> [1] "1" "2" "3"
#> $class
#> [1] "data.frame"

class(list_data_frame)
#> "data.frame"
当然,只有当列表中的元素具有相同的长度时,它才起作用:

## Creating an unequal list with data.frame attributes
wrong_list <- list(c(1,2,3), c(3,2,1,0))
attr(wrong_list, "names") <- c("A", "B")
attr(wrong_list, "row.names") <- c("1", "2", "3")
attr(wrong_list, "class") <- "data.frame"

wrong_list
#>   A B
#> 1 1 3
#> 2 2 2
#> 3 3 1
#> Warning message:
#> In format.data.frame(x, digits = digits, na.encode = FALSE) :
#>   corrupt data frame: columns will be truncated or padded with NAs

目前我所能提供的最好信息来自
?data.frame
:“列名应该是非空的,尝试使用空名称将产生不受支持的结果。”因此从某种意义上说,这是意料之中的,而不知道数据框如何工作的详细内部结构。@Suren当然可以,例如
名称(df)“没有列名的data.frames出现意外行为。“您期望的是什么?您可以在
Ops.data.frame
中找到特定的代码——0长度的“名称”用于在列上迭代,这将导致0列数据。frameOP虽然没有空名称,但至少没有名称。”all@Moody_Mudskipper它们不是一回事吗?事实上,我的错误是,两列都命名为
”“
是受支持的,所以看起来它们的真正意思是
NULL
@Suren名称可以是相同的,这就像预期的一样:`data.frame(a=1:5,a=5:9,check.names=FALSE)+1`@alko989是的,不确定我为什么没有检查它。谢谢修复了它。这是data.frames如何在幕后成为列表的一个透彻的解释。但它并没有真正回答为什么会断裂的问题。文档中明确指出不支持无名data.frames,但我想知道原因。我想得到一个警告,或者说R会暂时放上名字,然后在返回之前删除它们。
## Adding a data.frame class attribute
list_data_frame <- list3
attr(list_data_frame, "class") <- "data.frame"

## The attributes and class of the list
attributes(list_data_frame)
#> $names
#> [1] "A" "B"
#> $row.names
#> [1] "1" "2" "3"
#> $class
#> [1] "data.frame"

class(list_data_frame)
#> "data.frame"
## The dataframe
data_frame <-  data.frame("A" = c(1,2,3), "B" = c(3,2,1))
## The attributes and class of the list
attributes(data_frame)
#> $names
#> [1] "A" "B"
#> $row.names
#> [1] "1" "2" "3"
#> $class
#> [1] "data.frame"

class(data_frame)
#> "data.frame"

## "Converting" into a list
attr(data_frame, "class") <- NULL

attributes(data_frame)
#> $names
#> [1] "A" "B"
#> $row.names
#> [1] "1" "2" "3"

class(data_frame)
#> "list"
## Creating an unequal list with data.frame attributes
wrong_list <- list(c(1,2,3), c(3,2,1,0))
attr(wrong_list, "names") <- c("A", "B")
attr(wrong_list, "row.names") <- c("1", "2", "3")
attr(wrong_list, "class") <- "data.frame"

wrong_list
#>   A B
#> 1 1 3
#> 2 2 2
#> 3 3 1
#> Warning message:
#> In format.data.frame(x, digits = digits, na.encode = FALSE) :
#>   corrupt data frame: columns will be truncated or padded with NAs
## A list coerced into a data.frame without the right attributes
wrong_list <- list(c(1,2,3), c(3,2,1))
attr(wrong_list, "class") <- "data.frame"
wrong_list
#> NULL
#> <0 rows> (or 0-length row.names)