R:根据动态变化的变量

时间:2016-03-22 14:37:53

标签: r

我的数据集看起来像这样:

'data.frame':   60 obs. of  13 variables:
 $ File                       : Factor w/ 60 levels "EN_pp01","EN_pp03",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ F3.S1_mismatch.R.          : num  -0.507 -3.534 -3.022 -0.615 -0.372 ...
 $ F3.S2_position_mismatch    : num  -1.63 -2.37 -2.4 -1.8 -4.64 ...
 $ F3.S3_orientation_mismatch : num  0.966 -0.174 -3.205 0.361 -4.275 ...
 $ F3.S4_full_match           : num  -0.35 -2.124 -2.619 -0.984 -3.185 ...
 $ Fz.S1_mismatch.R.          : num  -0.763 -4.949 -3.765 -0.171 -2.668 ...
 $ Fz.S2_position_mismatch    : num  -1.88 -1.99 -2.37 -2.35 -6.26 ...
 $ Fz.S3_orientation_mismatch : num  0.895 -0.849 -2.944 -0.556 -6.287 ...
 $ Fz.S4_full_match           : num  -0.952 -2.644 -2.951 -1.288 -4.336 ...
 $ F4.S1_mismatch.R.          : num  -0.748 -3.943 -3.073 -0.529 -2.483 ...
 $ F4.S2_position_mismatch    : num  -1.75 -3.16 -1.46 -3.02 -6.13 ...
 $ F4.S3_orientation_mismatch : num  0.562 -1.57 -2.612 -1.785 -5.765 ...
 $ F4.S4_full_match           : num  -0.994 -3.185 -2.673 -1.974 -4.182 ...

这里的示例输入(我希望这有帮助):

> dput(DATA)
structure(list(File = structure(1:60, .Label = c("EN_pp01", "EN_pp03", 
"EN_pp04", "EN_pp05", "EN_pp06"), class = "factor"), F3.S1_mismatch.R. = c(-0.507205, 
-3.533596, -3.022342, -0.615138, -0.371956), F3.S2_position_mismatch = c(-1.628106, 
-2.372842, -2.397327, -1.803015, -4.642429, -3.497754),F3.S3_orientation_mismatch = c(0.965923, 
-0.17358, -3.204941, 0.361049, -4.275054), F3.S4_full_match = c(-0.349674, 
-2.124065, -2.618502, -0.984428, -3.185291), , Fz.S1_mismatch.R. = c(-0.76298, 
-4.94884, -3.765244, -0.170932, -2.667892), Fz.S2_position_mismatch = c(-1.875077, 
-1.993409, -2.370964, -2.353926, -6.26125),  Fz.S3_orientation_mismatch = c(0.894631, 
-0.848826, -2.943765, -0.556101, -6.287204), Fz.S4_full_match = c(-0.952075, 
-2.643912, -2.951059, -1.288112, -4.336169)), .Names = c("File", 
"F3.S1_mismatch.R.", "F3.S2_position_mismatch", "F3.S3_orientation_mismatch", 
"F3.S4_full_match", "Fz.S1_mismatch.R.", "Fz.S2_position_mismatch", 
"Fz.S3_orientation_mismatch", "Fz.S4_full_match"), row.names = c(1L, 
2L, 3L, 4L, 5L), class = "data.frame")

我想从前三个(S1,S2和S3)中减去每个第四列(总是以S4_full_match结尾,但以不同的电极名称开头)。由于我必须多次重复此减法(此处显示的示例数据集为3次)并更改每个列名的前几个字母,因此我创建了一个循环。循环应该创建我想要访问的相应列的名称(它确实如此),但是减法不起作用。它实际上并没有访问我指定的列的内容。如何正确访问列的内容以执行减法?

elec <- c("F3", "Fz", "F4")

for (i in 1:length(elec)) {
E = elec[i]

column1 <- c(paste(E, ".S1_mismatch", sep=''))
column2 <- c(paste( E, ".S4_full_match", sep='')) 
output <- DATA[,colnames(DATA)==column1[1]] - DATA[,colnames(DATA)==column2[1]] 
column1 <- output ## this is supposed to replace the original column content with the result of the subtraction 

column3 <- c(paste(E, ".S2_position_mismatch", sep=''))
output2 <- DATA[,colnames(DATA)==column3[1]] - DATA[,colnames(DATA)==column2[1]] 
column3 <- output2 

column4 <- c(paste(E, ".S3_orientation_mismatch", sep=''))
output3 <- DATA[,colnames(DATA)==column4[1]] - DATA[,colnames(DATA)==column2[1]] 
column4 <- output3
                }

提前感谢您的帮助。

0 个答案:

没有答案