我想从 data.frame 中删除一列。但是当我这样做时,data.frame 的一个属性丢失了,这是我不想要的。首先设置:
d <- data.frame(a = 1, b = 2, c = 3)
attr(d, "test_attribute") <- "something"
d2 <- d
d
#> a b c
#> 1 1 2 3
Run Code Online (Sandbox Code Playgroud)
该test属性存在:
attributes(d2) # contains $test_attribute [1] "something"
Run Code Online (Sandbox Code Playgroud)
现在我想删除第二列 - 但大多数方法都会破坏该属性:
attributes(d2[, -2]) # it's gone
attributes(dplyr::select(d2, -2)) # it's gone
Run Code Online (Sandbox Code Playgroud)
我找到了一种保存它的方法:
d3 <- d2
d3[2] <- NULL
attributes(d3)
Run Code Online (Sandbox Code Playgroud)
为什么test_attributeget 在前两种情况下会被删除,而在使用最后一种方法时不会?
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |