[英]Multiply values of some columns of a data frame within a date range and based on the values of another column in R
I am struggling with an ifelse statement in R. I have a data frame with 64 columns and 12861 rows.我正在为 R 中的 ifelse 语句苦苦挣扎。我有一个包含 64 列和 12861 行的数据框。 I need to multiply 10 of those columns within a date range (date is a column) and based on the value in another row (tdiff) and replace the old values with the new values in the data frame.
我需要将日期范围内的 10 列(日期是一列)并基于另一行中的值 (tdiff) 相乘,并用数据框中的新值替换旧值。 This line of code results in a dataframe that is 12681 X 12681. It should be the original size of 12681 (rows) by 64 (columns).
这行代码生成一个 12681 X 12681 的数据框。它应该是 12681(行)乘以 64(列)的原始大小。 What am I doing wrong?
我究竟做错了什么? Thank you in advance!
先感谢您!
test<-data.frame(ifelse(slopejoin$tdiff==2 & slopejoin$date>="2021-01-30" & slopejoin$date<="2021-08-08",
slopejoin[,c("a_Pinene","Isoprene","Methyl_Vinyl_Ketone","Nopinone","Benzene_", "D_Limonene",
"Sabinene","a_Cedrene","Pentane","Hexane")]*0.85,
slopejoin[,c("a_Pinene","Isoprene","Methyl_Vinyl_Ketone", "Nopinone","Benzene_", "D_Limonene",
"Sabinene","a_Cedrene","Pentane","Hexane")]))
cols <- "a_Pinene","Isoprene","Methyl_Vinyl_Ketone", "Nopinone","Benzene_", "D_Limonene", "Sabinene","a_Cedrene","Pentane","Hexane")
x <- "2021-01-30"
y <- "2021-08-08"
# Replace values in original fields
slopejoin[slopejoin$tdiff == 2 & slopejoin$date >= x & slopejoin$date >= y,cols] <- slopejoin[slopejoin$tdiff == 2 & slopejoin$date >= x & slopejoin$date >= y,cols] * 0.85
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.