我有一个带有变量(V1,V2,V5)权重的运行列表,我试图将其应用于输入文件,然后将输入文件打印到文本文档中。看起来这对于apply函数是一个很好的用途,但是我没有使用data.frame的子集进行矩阵乘法,然后将该函数放在apply函数中的经验。
# A list of runs with weights for 3 variables
run_table <- expand.grid(V1 = seq(0.5, 1.5, 0.5), V2 = seq(0.5, 1.5, 0.5), V5 = c(0,1))
# My input file (my input file has more columns and variables than this)
text1 <- c('various', 'text', 'values', 'for', 'input', 'files', 'a', 'b', 'c', 'd')
num <- c(4, 2, 2, 8, 16, 2, 8, 24, 8, 2)
name <- c('V1', 'V1', 'V1', 'V2', 'V3', 'V4', 'V4', 'V5', 'V5', 'V6')
input_file <- data.frame(text1, num, name)
# For each row in the run_table multiply the weight for variables V1, V2, & V5 by the value in input_file$num, then print the table to a text file
# Pseudo-code
for i to nrows(run_table)
run_table[i,]*input_file
write.table(input, file = paste0("input_", i, ".dat"), sep = " ", quote = F, row.names = F, col.names = F)