【发布时间】:2021-10-07 17:27:17
【问题描述】:
我想在函数内部创建一些名称来自函数参数的文件。文件内容将根据在正文函数中读取的文件中的某些条件创建
set.seed(123)
dt <- data.frame(col1_mean = sample(1:10, 10, TRUE), col2_n = sample(1:10, 10, TRUE), Col3 = c("ID1", "ID1","ID2", "ID1","ID2", "ID1","ID2", "ID1", "ID1", "ID1"), col4 = sample(1:10, 10, TRUE))
get_file = function (var1, var2) {
#create file name based on the var1 and var2 and the content based on the condition in dt
paste0("my_text_", var1, "_", var2) = dt[Col3 == paste0(var2)] # select only rows with ID given by var2
}
例如,get_file("AB", "ID1") 应该创建文件:my_text_AB_ID1,它将是 dt1 的子集,仅包含 Col3 中 ID1 的行。
上面的代码不起作用。任何想法 ? 谢谢
【问题讨论】: