用vapply分配新的数据框值

时间:2019-05-28 13:31:16

标签: r dataframe variable-assignment sapply

我正在尝试使用apply-type函数动态更新数据帧中的单元格。这是可复制的示例:

demo.df <- structure(list(node_id = 1:21, depth = c(4, 3, 2, 1, 0, 0,         
1, 0, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0), x_position = c(NA, NA, 
NA, NA, 1, 2, NA, 3, 4, NA, NA, 5, 6, NA, 7, 8, NA, NA, 9,10, 11), X1 
= 1:21, X2 = c(2L, 3L, 4L, 5L, NA, NA, 8L, NA, NA, 11L, 12L, NA, NA, 
15L, NA, NA, 18L, 19L, NA, NA, NA), X3 = c(17L, 10L, 7L, 6L, NA, NA, 
9L, NA, NA, 14L, 13L, NA, NA, 16L, NA, NA, 21L, 20L, NA, NA, NA)), 
class = "data.frame", row.names = c(1L, 2L, 4L, 8L, 16L, 17L, 9L, 18L, 
19L, 5L, 10L, 20L, 21L, 11L, 22L, 23L, 3L, 6L, 12L, 13L, 7L))

node_sequence <- c(4,7,11,14,18,3,10,17,2,1)

我想根据以下各列的平均值来更新 x_position ,以指定的顺序遍历各行,因此

x_spot <- function (x) 
 {mean(demo.df$x_position[which(demo.df$node_id%in%demo.df[x,c(4:6)])],
 na.rm = TRUE)
 }

此笨拙的代码可获得正确的结果:

demo.df$x_position[node_sequence[1]] <- x_spot(node_sequence[1])
demo.df$x_position[node_sequence[2]] <- x_spot(node_sequence[2])
demo.df$x_position[node_sequence[3]] <- x_spot(node_sequence[3])
demo.df$x_position[node_sequence[4]] <- x_spot(node_sequence[4])
demo.df$x_position[node_sequence[5]] <- x_spot(node_sequence[5])
demo.df$x_position[node_sequence[6]] <- x_spot(node_sequence[6])
demo.df$x_position[node_sequence[7]] <- x_spot(node_sequence[7])
demo.df$x_position[node_sequence[8]] <- x_spot(node_sequence[8])
demo.df$x_position[node_sequence[9]] <- x_spot(node_sequence[9])
demo.df$x_position[node_sequence[10]] <- x_spot(node_sequence[10])
demo.df$x_position

但是我无法弄清楚如何在 vapply 中为诸如此类的优雅

赋值
vapply(1:10, function(x) {demo.df$x_position[node_sequence[x]] <- x_spot(node_sequence[x])}, numeric(1))

我想念什么?也许 return()在代码中的某个地方?

1 个答案:

答案 0 :(得分:2)

由于先前的值用于计算下一个值,因此选项为<<-

vapply(1:10, function(x) {
  demo.df$x_position[node_sequence[x]] <<- x_spot(node_sequence[x])}, numeric(1))
demo.df$x_position

在这种情况下,for循环要好得多

for(i in seq_along(node_sequence)) {
   demo.df$x_position[node_sequence[i]] <- x_spot(node_sequence[i])
 }