【问题标题】:Function + Switch statement in R: EXPR must be a length 1 vectorR中的Function + Switch语句:EXPR必须是长度为1的向量
【发布时间】:2015-08-10 12:08:49
【问题描述】:

在下面的代码中,数据框离岸_Sites 已经存在并包含大约 1000 条记录。我正在构建一个函数,以便我可以将它重新用于我拥有的所有其他数据帧。

数据帧是从 SQL Server 获得的。目前我只得到了一个离岸站点,但其他的将以相同的方式生产。

这个想法是调用这个函数,里面有一个 switch 语句,根据数据帧,我将执行不同的转换。对于离岸站点之一,我需要连接一些字段,如示例中所示。

myStringConn <- "Driver=SQL Server;Server=SQL-SPATIAL;Database=AreasProt;Trusted_Connection=True;"
conn <- odbcDriverConnect(myStringConn)
offshore_Sites <- sqlQuery(conn, "select * from Offshore_Sites")

formatDataFrame <- function(dataframe) {
                              switch(dataframe, "offshore_Sites"  = {
                                offshore_sites <- as.data.table(offshore_Sites)
                                offshore_sites <- setnames(offshore_sites, 1:6, c("status","country","region","area","long","lat"))
                                offshore_sites <- unique(offshore_sites[, list(status,                                        
                                                                               country = paste(sort(unique(country)), collapse = ' & '),
                                                                               region = paste(sort(unique(region)), collapse = ' & '),
                                                                               area,
                                                                               long,
                                                                               lat), by = code])
                                })
                            }

formatDataFrame(offshore_Sites)

但是,当我运行它时,我得到了错误:

开关错误(数据框,offshore_Sites = { :
EXPR 必须是长度为 1 的向量

有人知道发生了什么吗?

【问题讨论】:

  • 最好提供一个可重现的例子。我注意到的一件事是setnames,您选择了 10 列并命名为 6。
  • 请在您的问题中包含您的data.frame (dput(head(df, 30))) 示例,并尝试详细说明您要完成的工作。
  • 首先,switch 将解析为长度为 1 的向量的表达式作为第一个值。您传递给它的数据帧长度不可能为 1,而且绝对不是向量.如果不了解更多关于 offshore_Sites 数据框中的内容以及您想对它做什么(以及您的其他数据框),我们将无法就如何处理它向您提供建议。
  • 你调用的函数是switch(EXPR, ...)。 R 期望您的第一个参数,在这种情况下 dataframe 是长度为 1 的向量。在 ?switch 示例中,这类似于“平均值”或“中值”或“修剪”。在示例中,R 然后根据传递的内容执行特定功能。
  • 感谢您的 cmets。我添加了更多信息。我需要将数据框同时传递给函数和 switch 语句。

标签: r


【解决方案1】:

我今天有了一些灵感,我有点发现问题出在哪里。该函数需要两个变量,数据框名称和数据框本身。

myStringConn <- "Driver=SQL Server;Server=SQL-SPATIAL;Database=AreasProt;Trusted_Connection=True;"
conn <- odbcDriverConnect(myStringConn)
offshore_Sites <- sqlQuery(conn, "select * from Offshore_Sites")

formatDataFrame <- function(dataframe, dataframeName) {
                            switch(dataframeName, "offshore_Sites"  = {
                                offshore_sites <- as.data.table(dataframe)
                                offshore_sites <- setnames(offshore_sites, 1:6, c("status","country","region","area","long","lat"))
                                offshore_sites <- unique(offshore_sites[, list(status,                                        
                                                                               country = paste(sort(unique(country)), collapse = ' & '),
                                                                               region = paste(sort(unique(region)), collapse = ' & '),
                                                                               area,
                                                                               long,
                                                                               lat), by = code])
                                })
                            }

formatDataFrame(offshore_Sites, "Offshore_Sites")

感谢所有 cmets :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多