首页 > 解决方案 > 使用运行矩阵创建多个输入文件

问题描述

我有一个包含变量(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)

标签: rapply

解决方案


推荐阅读