R 如何使用公式为字符串的dcast.data.table
我想对data.table使用cast,其公式的列名称为字符串 我的桌子:R 如何使用公式为字符串的dcast.data.table,r,data.table,reshape,R,Data.table,Reshape,我想对data.table使用cast,其公式的列名称为字符串 我的桌子: c1 c2 c3 1 A 1 1 B 2 1 C 3 2 A1 1 2 B1 2 2 C1 3 我想知道结果: c1 1 2 3 1 A B C 2 A1 B1 C1 我可以用命令来做这件事 dcast.data.table(dt,c1~c3,value.var=
c1 c2 c3
1 A 1
1 B 2
1 C 3
2 A1 1
2 B1 2
2 C1 3
我想知道结果:
c1 1 2 3
1 A B C
2 A1 B1 C1
我可以用命令来做这件事
dcast.data.table(dt,c1~c3,value.var=“c2”)
但我想在一个函数中运行dcast,该函数将c1列名的param作为字符串。比如说
f1 <- function(d, col_name1, col_name2, col_name3) {
dcast.data.table(d, col_name1 ~ col_name3, value.var = col_name2)
}
希望任何人都能帮忙
dcast
也接受公式作为字符串
f1 <- function(d, col_name1, col_name2, col_name3) {
dcast.data.table(d, paste(col_name1, "~", col_name3), value.var = col_name2)
}
f1(dt, "c1", "c2", "c3")
# c1 1 2 3
# 1: 1 A B C
# 2: 2 A1 B1 C1
f1C1
发生了什么事?@zx8754很抱歉,因为我提交了错误的问题。我已经编辑了我的问题,所以请删除-1。谢谢我没有投反对票。
f1 <- function(d, col_name1, col_name2, col_name3) {
dcast.data.table(d, paste(col_name1, "~", col_name3), value.var = col_name2)
}
f1(dt, "c1", "c2", "c3")
# c1 1 2 3
# 1: 1 A B C
# 2: 2 A1 B1 C1