【问题标题】:R - Issues while calling a user-defined functionR - 调用用户定义的函数时出现问题
【发布时间】:2018-07-09 23:33:04
【问题描述】:

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

> 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() 辅助函数的调用按我想要的方式工作。我将 data_sub$Key_P 中每个非 NA 值的“结果”作为数据框。

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

  Scorecard Results
1         1      12
2         3       8
3         5      11

我发现此链接与warning 相关,但它对解决我的问题没有帮助。

【问题讨论】:

    标签: r function


    【解决方案1】:

    以下作品

    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
    

    为了避免警告,我们需要在get_A 中去除带有as.numeric 的行名。

    另一个提示:最好将get_Pget_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)
    

    【讨论】:

    • 行得通!我继续在我的代码中修复了一些其他功能,以与您的编码技巧保持一致。试图进一步理解这一点,您能否解释一下为什么我的方法不起作用?另外,为什么我们没有在get_P 中收到警告,即使我们没有使用as.numeric
    • @Sujith 在get_P 我们得到一个列向量,而在get_A 我们得到一个row vector(准确地说,是一个1x3@ 987654335@)。所以我们需要用as.numeric.将行向量(data.frame)转换成数值(列)向量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多