gpt4 book ai didi

r - dplyr 0.3.0.2 rename() 习惯用法在加载 reshape 包时不稳定

转载 作者:行者123 更新时间:2023-12-04 10:00:05 33 4
gpt4 key购买 nike

sessionInfo()
# R version 3.1.1 (2014-07-10)
# Platform: x86_64-apple-darwin10.8.0 (64-bit)
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] dplyr_0.3.0.2
#
# loaded via a namespace (and not attached):
# [1] assertthat_0.1 DBI_0.3.1 lazyeval_0.1.9 magrittr_1.0.1 parallel_3.1.1 Rcpp_0.11.3
# [7] tools_3.1.1

当仅加载 dplyr 0.3.0.2 时,默认的重命名习语有效。
packageVersion("dplyr")
# [1] ‘0.3.0.2’
iris[1:10,] %>% rename(petal_length = Petal.Length)
# Sepal.Length Sepal.Width petal_length Petal.Width Species
# 1 5.1 3.5 1.4 0.2 setosa
# 2 4.9 3.0 1.4 0.2 setosa

加载 library(reshape) 时,习语似乎不起作用
# other attached packages:
# [1] reshape_0.8.5 dplyr_0.3.0.2
#
# loaded via a namespace (and not attached):
# [1] assertthat_0.1 chron_2.3-45 data.table_1.9.5 DBI_0.3.1 lazyeval_0.1.9
# [6] magrittr_1.0.1 parallel_3.1.1 plyr_1.8.1 Rcpp_0.11.3 reshape2_1.4
# [11] stringr_0.6.2 tidyr_0.1 tools_3.1.1

iris[1:10,] %>% rename(petal_length = Petal.Length)
# Error in rename(`iris[1:10, ]`, petal_length = Petal.Length) :
# unused argument (petal_length = Petal.Length)

并且需要使用下面的代码
iris[1:10,] %>% rename(c("Petal.Length" = "petal_length"))
# Sepal.Length Sepal.Width petal_length Petal.Width Species
# 1 5.1 3.5 1.4 0.2 setosa
# 2 4.9 3.0 1.4 0.2 setosa

这是一个错误吗?

最佳答案

我推荐data.table::setnames功能。

iris %>% 
data.table::setnames(
old = "Petal_length",
new = "petal_length") %>%
head

关于r - dplyr 0.3.0.2 rename() 习惯用法在加载 reshape 包时不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26371279/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com