Sapply意外返回-列表矩阵?

时间:2019-04-18 09:04:39

标签: r matrix sapply

我有一个应从数据帧更新某些值的函数。 该函数获取要更改的数据,数据框以及变量的行索引。 sapply调用此函数以更改所有索引。 我希望矩阵可以作为sapply的返回,并且希望能够通过索引来更改基本数据框。 不幸的是,我不能提供真实的数据,但是下面的虚拟数据重复了这个问题。 我不明白为什么它不起作用。 可能是因为sapply返回的矩阵不是“正常”矩阵 整数,但某种列表矩阵?

虚拟数据框:

data_test = data.frame(matrix(1:100, 10, 10))
names(data_test) = paste0("Var", 1:10)

# Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
# 1     1   11   21   31   41   51   61   71   81    91
# 2     2   12   22   32   42   52   62   72   82    92
# 3     3   13   23   33   43   53   63   73   83    93
# 4     4   14   24   34   44   54   64   74   84    94
# 5     5   15   25   35   45   55   65   75   85    95
# 6     6   16   26   36   46   56   66   76   86    96
# 7     7   17   27   37   47   57   67   77   87    97
# 8     8   18   28   38   48   58   68   78   88    98
# 9     9   19   29   39   49   59   69   79   89    99
# 10   10   20   30   40   50   60   70   80   90   100

“普通”矩阵,用索引替换data_test的某些值:

data_replace = matrix(1:16, 4,4)

# [,1] [,2] [,3] [,4]
# [1,]    1    5    9   13
# [2,]    2    6   10   14
# [3,]    3    7   11   15
# [4,]    4    8   12   16

这很好用:

data_test[c("Var3", "Var4", "Var5", "Var6")][4:7,] = data_replace

# Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
# 1     1   11   21   31   41   51   61   71   81    91
# 2     2   12   22   32   42   52   62   72   82    92
# 3     3   13   23   33   43   53   63   73   83    93
# 4     4   14    1    5    9   13   64   74   84    94
# 5     5   15    2    6   10   14   65   75   85    95
# 6     6   16    3    7   11   15   66   76   86    96
# 7     7   17    4    8   12   16   67   77   87    97
# 8     8   18   28   38   48   58   68   78   88    98
# 9     9   19   29   39   49   59   69   79   89    99
# 10   10   20   30   40   50   60   70   80   90   100

虚拟函数由sapply调用:

# just a test-function to be called by sapply. It multiplies all the temp_vars from temp_data in rows temp_index by 2
function_test = function(temp_index, temp_data = data_test, temp_vars = c("Var3", "Var4", "Var5", "Var6"))
{
  return(temp_data[temp_vars][temp_index,] * 2)
}

在某些行索引上使用sapply进行函数调用

#function call 
temp_results = t(sapply(4:7, function_test))
data_test[c("Var3", "Var4", "Var5", "Var6")][4:7,]

# Var3 Var4 Var5 Var6
# 4    1    5    9   13
# 5    2    6   10   14
# 6    3    7   11   15
# 7    4    8   12   16

不幸的是,尝试使用索引替换data_test的某些值(就像以前一样)在这里不起作用。 它给了我以下错误:

data_test[c("Var3", "Var4", "Var5", "Var6")][4:7,] = temp_results

# Warning message:
# In `[<-.data.frame`(`*tmp*`, 4:7, , value = list(Var3 = c(21, 22,  :
#   provided 16 variables to replace 4 variables

有关已使用数据的一些信息。 它们的大小都应该相同。

dim(data_test[c("Var3", "Var4", "Var5", "Var6")][4:7,])
# 4 4
dim(temp_results)
# 4 4
dim(data_replace)
# 4 4

class(temp_results)
# matrix
class(data_replace)
# matrix

这对我来说很奇怪。我不明白那是什么意思。

apply(temp_results, 2, class)
# Var3   Var4   Var5   Var6 
# "list" "list" "list" "list" 
apply(data_replace, 2, class)
# "integer" "integer" "integer" "integer"

我可以通过使用temp_results = lapply而不是temp_results = sapply来解决此问题,然后再使用matrix(unlist(temp_results), 4, byrow = T)函数,但是我仍然想了解返回结果的。

任何输入都会很棒!预先感谢

1 个答案:

答案 0 :(得分:1)

让您的函数将值作为矩阵返回:

function_test <- function(temp_index, temp_data = data_test, temp_vars = c("Var3", "Var4", "Var5", "Var6"))
{
  return(as.matrix(temp_data[temp_vars][temp_index,] * 2))
}

它应该按照您希望的方式工作。