R 将行中的其他值与行中的第一个值相乘

R 将行中的其他值与行中的第一个值相乘,r,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"

我有以下数据框:

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)

Date一旦将值设置为非字符(不在“”中),就可以像这样使用apply:

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

newNo
apply
needed,只需乘以即可。但首先让我们把你的数据整理得很好

它们是定义数据的方式,因为在数字周围使用引号,所以所有应该是数字的列都是因子。我们使用
lappy
将它们安全地转换为数字:

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

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

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

为什么要将浓度和排放值作为字符(在数据框中放入时会成为因子,而不将
stringsafactors
定义为FALSE)?如果数据格式正确(即数字),只需按照Ryan所说的
y[,3:6]@Matt
ncol(y)
给出列数
y[,3:ncol(y)]
@Matt,
ncol(y)
提供列数。如果感兴趣的列不一定是最后一列,而是具有相同类型的名称,您也可以使用
grep
grep(“^concentration”,colnames(y))
最好在
数据框上使用
lappy
apply
强制
数据。帧
矩阵
矢量化有助于列的每个元素乘以向量的每个元素(
排放
),但这要归功于
回收
(排放
向量的)
)我们不需要
lappy
如果浓度列没有相同的前缀但名称不同,我想让R检测列数而不是
[1:4]
?然后执行类似
concentration\u index=grep(pattern=“concentration”,names(y))
concentration\u names=names(y)[grepl]的操作(pattern=“concentration”,names(y))]
。您可以调整
pattern
参数以使用需要的特定正则表达式。甚至
grep(,value=TRUE)
y[, 2] * y[, -(1:2)]