0

我一直在尝试创建一个函数,通过编写以下代码来提取给定数据框中给定列的唯一值:

val_uniques <- function(colname, datframe)
  if colname %in% colnames(dataframe) {
    print(unique(dataframe[, colname], incomparables = FALSE))
  } else {
    print("cette colonne n'existe pas")
  }

但不幸的是,我不断收到此错误:

print(unique(dataframe[,colname] , incomparables = FALSE))} else { print("cette Colonne n'existe pas")} 错误:“print(unique(dataframe[,colname], incomparables =错误的))}”

我知道这是一个愚蠢的问题,因为它与}in ifor有关else,但我已经尝试了所有方法,但没有成功。

PS 这是我第一次用 R 编程。

4

1 回答 1

0

对象名称中有一些拼写错误,datframe并且dataframe大括号放错了位置:

val_uniques <- function(colname, dataframe) {
  if (colname %in% colnames(dataframe)) {
    print(unique(dataframe[, colname] , incomparables = FALSE))
  } else {
    print("cette colonne n'existe pas")
  }
}

df <- data.frame(a = c(1, 1, 3, 4), b = 1:4)

val_uniques("a", df)
# [1] 1 3 4
于 2018-09-29T21:22:58.993 回答