我有一个 tibble
, df
:
> df
# A tibble: 4 x 5
profile Sepal.Length Sepal.Width Petal.Length Petal.Width
<chr> <dbl> <dbl> <dbl> <dbl>
1 Profile 1 -1.011 0.850 -1.301 -1.251
2 Profile 2 0.542 -0.389 0.662 0.673
3 Profile 3 -0.376 -0.967 0.115 0.038
4 Profile 4 1.502 0.158 1.277 1.239
当我使用`tidyr::gather()时,如下:
tidyr::gather(df, var, val, -profile)
返回以下错误:
Warning message: attributes are not identical across measure variables; they will be dropped
我做了一些搜索(并检查了
df
是否有任何可能导致问题的属性),但不明白为什么要打印此警告。df <- structure(list(profile = c("Profile 1", "Profile 2", "Profile 3",
"Profile 4"), Sepal.Length = structure(c(-1.011, 0.542, -0.376,
1.502), .Dim = c(150L, 1L), "`scaled:center`" = 5.84333333333333, "`scaled:scale`" = 0.828066127977863),
Sepal.Width = structure(c(0.85, -0.389, -0.967, 0.158), .Dim = c(150L,
1L), "`scaled:center`" = 3.05733333333333, "`scaled:scale`" = 0.435866284936698),
Petal.Length = structure(c(-1.301, 0.662, 0.115, 1.277), .Dim = c(150L,
1L), "`scaled:center`" = 3.758, "`scaled:scale`" = 1.76529823325947),
Petal.Width = structure(c(-1.251, 0.673, 0.038, 1.239), .Dim = c(150L,
1L), "`scaled:center`" = 1.19933333333333, "`scaled:scale`" = 0.762237668960347)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -4L), .Names = c("profile",
"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))
编辑:
当我打印
df
,看起来不错:> df
# A tibble: 2 x 5
profile Sepal.Length Sepal.Width Petal.Length Petal.Width
<chr> <dbl> <dbl> <dbl> <dbl>
1 Profile 1 -1.011 0.850 -1.301 -1.251
2 Profile 2 0.506 -0.425 0.650 0.625
但是,当我运行
dput(df)
时,然后运行它输出的代码(与上面相同的代码),返回@neilfws标识的错误。
最佳答案
我最近遇到了这个,并且有一些我可以补充的想法,这些想法可能有助于其他人将来引用。
同意@neilfws,属性错误之一是由于具有 .Dim = c(150, 1L)
在列属性中 - 指的是 iris
的 150 行数据,但这个子集只有 4 行数据。
但是,返回的警告(也在帖子标题中):
Warning message: attributes are not identical across measure variables; they will be dropped
指组合
data.frame
时删除的属性带有 gather
的列,因为它们并不完全相同。 df
在这篇文章中确实有从 scaled
出现的属性:R> str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 5 variables:
$ profile : chr "Profile 1" "Profile 2" "Profile 3" "Profile 4"
$ Sepal.Length: num [1:4, 1] -1.011 0.542 -0.376 1.502
..- attr(*, "`scaled:center`")= num 5.84
..- attr(*, "`scaled:scale`")= num 0.828
$ Sepal.Width : num [1:4, 1] 0.85 -0.389 -0.967 0.158
..- attr(*, "`scaled:center`")= num 3.06
..- attr(*, "`scaled:scale`")= num 0.436
$ Petal.Length: num [1:4, 1] -1.301 0.662 0.115 1.277
..- attr(*, "`scaled:center`")= num 3.76
..- attr(*, "`scaled:scale`")= num 1.77
$ Petal.Width : num [1:4, 1] -1.251 0.673 0.038 1.239
..- attr(*, "`scaled:center`")= num 1.2
..- attr(*, "`scaled:scale`")= num 0.762
但这只是一个警告 - 它仍然可以工作,但您会丢失
attr
信息,因为它在组合时因变量而异。如果您有类似的
data.frame
没有这些 attr
- 例如:structure(list(profile = structure(1:4, .Label = c("Profile 1",
"Profile 2", "Profile 3", "Profile 4"), class = "factor"), Sepal.Length = c(-1.011,
0.542, -0.376, 1.502), Sepal.Width = c(0.85, -0.389, -0.967,
0.158), Petal.Length = c(-1.301, 0.662, 0.115, 1.277), Petal.Width = c(-1.251,
0.673, 0.038, 1.239)), class = "data.frame", row.names = c(NA,
-4L))
然后
gather
将在没有任何警告的情况下工作。此外,截至最新
tidyr
,建议使用pivot_longer
而不是 gather
如 gather
不会被维护。library(tidyr)
pivot_longer(df, cols = -profile, names_to = "var", values_to = "val")
关于r - 是什么导致了 tidyr 警告消息 : "attributes are not identical across measure variables",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45821170/