首页 > 解决方案 > R - 调用用户定义函数时出现问题

问题描述

我有以下名为“数据集”的数据框

> dataset
   V1 V2 V3 V4 V5 V6   V7
1   A 29 27  0 14 21  163
2   W 70 40 93 63 44 1837
3   E 11  1 11 49 17  315
4   S 20 59 36 23 14  621
5   C 12  7 48 24 25  706
6   B 14  8 78 27 17  375
7   G 12  7  8  4  4  257
8   T  0  0  0  0  0    0
9   N 32  6  9 14 17  264
10  R 28 46 49 55 38  608
11  O 12  2  8 12 11  450

我有两个辅助函数如下

get_A <- function(p){  
     return(data.frame(Scorecard = p, 
                       Results = dataset[nrow(dataset),(p+1)]))
 }  #Pulls the value from the last row for a given value of (p and offset by  1)

get_P <- function(p){
     return(data.frame(Scorecard= p, 
                       Results = dataset[p,ncol(dataset)]))
} #Pulls the value from the last column for a given value of p

我有以下数据框,我需要在其上运行上述辅助函数。会有 NA,因为我正在从一个 excel 文件中读取这个“data_sub”数据帧,该文件的两列可能有不相等的行。

> data_sub
      Key_P     Key_A
1         2         1
2         3         3
3         4         5
4        NA        NA

当我调用辅助函数时,我得到一些奇怪的结果,如下所示:

> get_P(data_sub[complete.cases(data_sub$Key_P),]$Key_P)
  Scorecard Results
1         2    1837
2         3     315
3         4     621

> get_A(data_sub[complete.cases(data_sub$Key_A),]$Key_A)
  Scorecard Results.V2 Results.V4 Results.V6
1         1         12          8         11
2         3         12          8         11
3         5         12          8         11
Warning message:
In data.frame(Scorecard = p, Results = dataset[nrow(dataset), (p +  :
  row names were found from a short variable and have been discarded

对辅助函数的调用按get_P()我想要的方式工作。我将每个非 NA 值的“结果”data_sub$Key_P作为数据框获取。

但是对get_A()辅助函数的调用给出了奇怪的结果和一个警告。我希望它给出一个与给定调用类似的数据框get_P()。为什么会发生这种情况,我该如何get_A()提供正确的数据框?基本上,这个的输出应该是

  Scorecard Results
1         1      12
2         3       8
3         5      11

我发现这个链接与警告有关,但它对解决我的问题没有帮助。

标签: rfunction

解决方案


以下作品

get_P <- function(df, data_sub) {
    data_sub <- data_sub[complete.cases(data_sub), ]
    data.frame(
        Scorecard = data_sub$Key_P,
        Results = df[data_sub$Key_P, ncol(df)])
}
get_P(df, data_sub)
#  Scorecard Results
#1         2    1837
#2         3     315
#3         4     621

get_A <- function(df, data_sub) {
    data_sub <- data_sub[complete.cases(data_sub), ];
    data.frame(
        Scorecard = data_sub$Key_A,
        Results = as.numeric(df[nrow(df), data_sub$Key_A + 1]))
}
get_A(df, data_sub)
#  Scorecard Results
#1         1      12
#2         3       8
#3         5      11

为了避免警告,我们需要用as.numericin去除行名get_A

另一个提示:最好的编码实践是get_P同时get_A使用两者dfdata_sub避免全局变量。


样本数据

df <- read.table(text =
    "   V1 V2 V3 V4 V5 V6   V7
1   A 29 27  0 14 21  163
2   W 70 40 93 63 44 1837
3   E 11  1 11 49 17  315
4   S 20 59 36 23 14  621
5   C 12  7 48 24 25  706
6   B 14  8 78 27 17  375
7   G 12  7  8  4  4  257
8   T  0  0  0  0  0    0
9   N 32  6  9 14 17  264
10  R 28 46 49 55 38  608
11  O 12  2  8 12 11  450", header = T, row.names = 1)


data_sub <- read.table(text =
    "      Key_P     Key_A
1         2         1
2         3         3
3         4         5
4        NA        NA", header = T, row.names = 1)

推荐阅读