将第一个值与第一个值相乘

时间:2018-07-09 13:37:03

标签: r

我有以下数据框:

Date <- c("04.06.2013","05.06.2013","06.06.2013","07.06.2013","08.06.2013","09.06.2013")
 discharge <- c("1000","2000","1100","3000","1700","1600")
 concentration_1 <- c("25","20","11","6.4","17","16")
 concentration_2 <- c("1.4","1.7","2.7","3.2","4","4.7")
 concentration_3 <- c("1.2","1.3","1.9","2.2","2.4","3")
 concentration_4 <- c("1","0.92","2.5","3","3.4","4.8")

y <- data.frame(Date, discharge,concentration_1,concentration_2,concentration_3,concentration_4, stringsAsFactors=FALSE)
y$Date <- as.Date(y$Date, format ="%d.%m.%Y")
y[-1] <- sapply(y[-1], as.numeric)

在每一行中,我需要将每种浓度乘以放电。 我当时在研究apply函数,但找不到解决方法。

3 个答案:

答案 0 :(得分:5)

不需要apply,只需相乘即可。但是首先让我们将数据保持得体。

使用它们定义数据的方式,因为您在数字两边使用了引号,所以所有应该为数字的列都是因数。我们使用lapply将其安全地转换为数字:

y <- data.frame(Date, discharge,concentration_1,concentration_2,concentration_3,concentration_4)
y$Date <- as.Date(y$Date, format ="%d.%m.%Y")
str(y)
# 'data.frame': 6 obs. of  6 variables:
#  $ Date           : Date, format: "2013-06-04" "2013-06-05" "2013-06-06" "2013-06-07" ...
#  $ discharge      : Factor w/ 6 levels "1000","1100",..: 1 5 2 6 4 3
#  $ concentration_1: Factor w/ 6 levels "11","16","17",..: 5 4 1 6 3 2
#  $ concentration_2: Factor w/ 6 levels "1.4","1.7","2.7",..: 1 2 3 4 5 6
#  $ concentration_3: Factor w/ 6 levels "1.2","1.3","1.9",..: 1 2 3 4 5 6
#  $ concentration_4: Factor w/ 6 levels "0.92","1","2.5",..: 2 1 3 4 5 6

# convert all columns but the first safely to numeric
y[, -1] = lapply(y[, -1], function(x) as.numeric(as.character(x)))
str(y)
# 'data.frame': 6 obs. of  6 variables:
#  $ Date           : Date, format: "2013-06-04" "2013-06-05" "2013-06-06" "2013-06-07" ...
#  $ discharge      : num  1000 2000 1100 3000 1700 1600
#  $ concentration_1: num  25 20 11 6.4 17 16
#  $ concentration_2: num  1.4 1.7 2.7 3.2 4 4.7
#  $ concentration_3: num  1.2 1.3 1.9 2.2 2.4 3
#  $ concentration_4: num  1 0.92 2.5 3 3.4 4.8

完成后,我们可以将浓度列乘以排放列。 R将“回收”排放塔以适当地乘以每个浓度塔。

concentration_columns = paste0("concentration_", 1:4)
y[, concentration_columns] = y[, concentration_columns] * y[, "discharge"]
y
#        Date discharge concentration_1 concentration_2 concentration_3 concentration_4
# 1 2013-06-04      1000           25000            1400            1200            1000
# 2 2013-06-05      2000           40000            3400            2600            1840
# 3 2013-06-06      1100           12100            2970            2090            2750
# 4 2013-06-07      3000           19200            9600            6600            9000
# 5 2013-06-08      1700           28900            6800            4080            5780
# 6 2013-06-09      1600           25600            7520            4800            7680

答案 1 :(得分:2)

乘法是矢量化的,只需将要乘法的列用作操作数即可。

y[, 2] * y[, -(1:2)]

答案 2 :(得分:0)

一旦您将值设置为非字符(不在“”中),则可以使用apply来实现:

   new <- data.frame(y[,1:2],apply(y[,3:6],2,function(x) x*y$discharge))