【问题标题】:Error: could not find function "distinct" when using dplyr library for R on Windows 7 [closed]错误:在 Windows 7 上为 R 使用 dplyr 库时找不到函数“distinct”[关闭]
【发布时间】:2015-02-25 23:25:20
【问题描述】:

我想从数据框中的列中获取唯一值。用R包dplyr应该是可以的。

这个distinct(select(dataframe, column)) 在我的 Mac 上运行良好。在 Windows 7 上的 RStudio 中,我遇到了这个问题:

当我运行这个 R 代码时:

library(dplyr)
df <- data.frame(replicate(4,sample(0:1,10,rep=TRUE)))

unique_values <- distinct(select(df, X1))

编辑

请检查dplyr::distinct(select(df, X1)) 是否有效? ——阿克伦

当然 - 这是控制台输出:

编辑

我没有使用 distinct,但也许 unique 对你有用? unique(df$X1) - NPE

它确实有效,而且也很简洁!我仍然想了解这个 dplyr 错误...

编辑

请改为添加sessionInfo() 的输出。 ——罗兰

编辑

一些 cmets 注意到 dplyr_0.2 版本是旧的。 install.packages("dplyr") 获取到旧包的 CRAN 链接。现在来弄清楚如何手动安装dplyr_0.3.0.2

【问题讨论】:

  • 请检查dplyr::distinct(select(df, X1)) 是否有效?
  • 我没用过distinct,但也许unique 对你有用? unique(df$X1)
  • 你提供了很多多余的信息。请改为添加sessionInfo() 的输出。
  • library(dplyr)了吗?
  • distinct 在 0.3.0.2

标签: r dplyr versions


【解决方案1】:

想通了!旧的R 表示旧的dplyr 表示没有distinct() 功能。

要解决此问题,请安装最新版本的 R:

  1. 转至http://www.r-project.org
  2. 点击“CRAN”
  3. 然后选择您喜欢的 CRAN 站点。我喜欢堪萨斯州:http://rweb.quant.ku.edu/cran/
  4. 点击“为 X 下载 R”[其中 X 是您的操作系统]
  5. 按照您的操作系统的安装过程进行操作
  6. 重启 RStudio
  7. 欢喜

来源:这很好answer

然后在 RStudio 控制台中运行命令install.packages("dplyr")

现在您可以创建一个数据框并使用distinct() 函数从其中一列中获取唯一值:

library(dplyr)

# create a dataframe with some values
df <- data.frame(replicate(4,sample(0:1,10,rep=TRUE)))
df

# select a column from that dataframe and get a list of the unique values
unique_values <- distinct(select(df, X1))
unique_values

在控制台中你应该看到:

感谢David ArenburgRichard Scriven 指出我们的dplyr-0.2 是旧的并且缺少distinct() 功能。这种思路引出了答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2019-10-14
    • 2018-02-17
    • 2011-10-24
    • 2020-03-15
    相关资源
    最近更新 更多