R中的乘法数据集

时间:2014-07-16 13:03:46

标签: r multiplication

我正在手动进行Swait-Louviere测试,我想将我的数据集与一个数字相乘,然后运行我的模型。最简单的方法是什么?

     price1 price2 price3 Protest dset Protest1 chid prod
9.1       25     50     35       0    1        0    9    1
9.2       25     50     35       0    1        0    9    0
9.SQ      25     50     35       0    1        0    9    1
10.1      85     65     35       0    1        0   10    1
10.2      85     65     35       0    1        0   10    0
10.SQ     85     65     35       0    1        0   10    1
      salm orig camp price asc1 asc2 asc3   s
9.1      1    0    1    25    1    0    0 0.2
9.2      0    1    0    50    0    1    0 0.2
9.SQ     1    1    0    35    0    0    1 0.2
10.1     1    1    0    85    1    0    0 0.2
10.2     0    0    1    65    0    1    0 0.2
10.SQ    1    1    0    35    0    0    1 0.2

基本上,我需要乘以1.1

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

mydf = structure(list(x = structure(c(4L, 5L, 6L, 1L, 2L, 3L), .Label = c("10.1", 
+ "10.2", "10.SQ", "9.1", "9.2", "9.SQ"), class = "factor"), price1 = c(25L, 
+ 25L, 25L, 85L, 85L, 85L), price2 = c(50L, 50L, 50L, 65L, 65L, 
+ 65L), price3 = c(35L, 35L, 35L, 35L, 35L, 35L), Protest = c(0L, 
+ 0L, 0L, 0L, 0L, 0L), dset = c(1L, 1L, 1L, 1L, 1L, 1L), Protest1 = c(0L, 
+ 0L, 0L, 0L, 0L, 0L), chid = c(9L, 9L, 9L, 10L, 10L, 10L), prod = c(1L, 
+ 0L, 1L, 1L, 0L, 1L)), .Names = c("x", "price1", "price2", "price3", 
+ "Protest", "dset", "Protest1", "chid", "prod"), class = "data.frame", row.names = c(NA, 
+ -6L))
> 
mydf
      x price1 price2 price3 Protest dset Protest1 chid prod
1   9.1     25     50     35       0    1        0    9    1
2   9.2     25     50     35       0    1        0    9    0
3  9.SQ     25     50     35       0    1        0    9    1
4  10.1     85     65     35       0    1        0   10    1
5  10.2     85     65     35       0    1        0   10    0
6 10.SQ     85     65     35       0    1        0   10    1
for(i in 2:ncol(mydf)) mydf[,i]= mydf[,i]*1.1
mydf 
      x price1 price2 price3 Protest dset Protest1 chid prod
1   9.1   27.5   55.0   38.5       0  1.1        0  9.9  1.1
2   9.2   27.5   55.0   38.5       0  1.1        0  9.9  0.0
3  9.SQ   27.5   55.0   38.5       0  1.1        0  9.9  1.1
4  10.1   93.5   71.5   38.5       0  1.1        0 11.0  1.1
5  10.2   93.5   71.5   38.5       0  1.1        0 11.0  0.0
6 10.SQ   93.5   71.5   38.5       0  1.1        0 11.0  1.1
> 

你也可以使用:

mydf[,2:9] =mydf[,2:9]*1.1