readr::read_csv
添加了在编辑数据时不会更新的属性。例如,
library('tidyverse')
df <- read_csv("A,B,C\na,1,x\nb,1,y\nc,1,z")
# Remove columns with only one distinct entry
no_info <- df %>% sapply(n_distinct)
no_info <- names(no_info[no_info==1])
df2 <- df %>%
select(-no_info)
检查结构,我们发现df2
的属性中仍然存在B列:
> str(df)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: num 1 1 1
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> str(df2)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 2 variables:
$ A: chr "a" "b" "c"
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> attributes(df2)
$class
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
$row.names
[1] 1 2 3
$spec
cols(
A = col_character(),
B = col_double(),
C = col_character()
)
$names
[1] "A" "C"
>
如何删除列(或数据的任何其他更新)并使更改准确地反映在新的数据结构和属性中?
答案 0 :(得分:1)
您可以通过将列规范设置为NULL
来删除列规范:
> attr(df, 'spec') <- NULL
> str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: int 1 1 1
$ C: chr "x" "y" "z"
> df
# A tibble: 3 x 3
A B C
<chr> <int> <chr>
1 a 1 x
2 b 1 y
3 c 1 z
答案 1 :(得分:0)
对我来说这是有效的(R 版本 4.0.5 (2021-03-31)):
> attr(data, "class")
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
> attr(data, "class") <- attr(data, "class")[-1]
> attr(data, "class")
[1] "tbl_df" "tbl" "data.frame"