我正在尝试使用多参数为数据帧计算新列 功能。当这个功能很简单时,一切都很好。当那个 函数调用另一个函数,事情出错了。
# The goal is to transform Foo into a new column.
df <- data.frame(
Year =c(1901,1901,1900,1902),
Month=c( 2, 1, 2, 1),
Foo =c( 1, 2, 3, 4)
)
# This simple multi-arg transform works fine.
Foo2Baz <- function(year, month, foo) {
return(year + month + foo)
}
df$Baz <- Foo2Baz(df$Year, df$Month, df$Foo)
# Expected Baz column: [1904, 1904, 1905, 1907]
df$Baz
[1] 1904 1904 1905 1907
失败的更有趣的变换就像上面的Foo2Baz, 除了它将调用另一个从某些人查找值的函数 其他表。
# First we load a dataframe that backs the lookup operation. It's nice
# to keep as a dataframe, as opposed to matrix, for human inspection
# of cells.
lookup_table <- data.frame(
Year=c(1900, 1901, 1902),
Jan =c( 10, 20, 30),
Feb =c( 100, 200, 200))
# Then we define a function to lookup a cell.
Lookup <- function(year, month) {
return(lookup_table[
year - 1899, # converts year to row index
1 + month # converts month to column, skipping 'Year'
])
}
# We expect a lookup of 1901-Feb to be 200
Lookup(1901, 2)
[1] 200
# The goal is to transform Foo into new column Bar
# by looking up that case's Year and Month.
Foo2Bar <- function(year, month, foo) {
return(foo + Lookup(year, month))
}
# We expect case 1 (1901,Feb,Foo=1) to have Bar=201
Foo2Bar(1901,2,1)
[1] 201
# We expect case 4 (1902,Jan,Foo=4) to have Bar=34
Foo2Bar(1902,1,4)
[1] 34
似乎我们现在可以像使用Foo2Bar一样计算Bar列 我们使用了更简单的Foo2Baz:
df$Bar <- Foo2Bar(df$Year, df$Month, df$Foo)
df$Bar
Feb Jan Feb.1 Jan.1
2 201 21 201 21
2.1 202 22 202 22
1 103 13 103 13
3 204 34 204 34
没有专栏?而是一个矩阵,其中行和列看起来像我们发送到Lookup()函数的各种输入?!
我尝试过使用do.call,apply,lapply,sapply和dplyr mutate。我似乎只是缺少一些基本的东西。
答案 0 :(得分:1)
我们只需要cbind
Lookup <- function(year, month) {
lookup_table[cbind(
year - 1899,
1 + month)
]
}
Lookup(1901, 2)
#[1] 200
Foo2Bar(1901,2,1)
#[1] 201
df$Bar <- Foo2Bar(df$Year, df$Month, df$Foo)
df
# Year Month Foo Bar
#1 1901 2 1 201
#2 1901 1 2 22
#3 1900 2 3 103
#4 1902 1 4 34