简体   繁体   English

将日期范围内数据框某些列的值相乘,并基于 R 中另一列的值

[英]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.

相关问题 在 R 中将具有日期范围的列中的值相乘 - Multiply values within a column with a date range in R R-根据一列中跨不同列的公共值,将data.frame格式化为另一个“组合” data.frame - R- format a data.frame into another 'combined' data.frame based on common values within a column dependent across different columns 根据R中另一个数据帧中存储的值对数据帧中的列求和 - summing columns in a data frame based on the values stored within another data frame in R 如何通过滞后日期和 ID 根据另一个数据框替换数据框中某列的某些值? - How to replace some values of a column in a data frame based on another data frame through a lagged date and ID? 在R中的条件下,将一个data.frame中的列值乘以另一个data.frame中的列 - Multiply column values in one data.frame by column in another data.frame on a condition in R 根据列中的值范围在R中子集数据帧 - Subsetting data frame in R based on range of values in a column R - select 年基于其他列的计算值在数据框中 - R - select years within a data frame based on calculated values from another columns 根据R中另一列的值乘以数据框列的值 - Multiplying data frame column values based on the value of another column in R R:根据另一列操作一个数据框列的值 - R: Manipulate values of one data frame column based on another column 如何基于一列的部分与另一数据框中的值的匹配来填充R中的列 - How to fill columns in R based on matching parts of one column to values in another data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM