Multiplication in R of a given record period of a dataframe

I have a dataset from 1966 to 2002, I want change the units(multiply values by 0.305) of some of the values in the dataframe from 1966/05/07 to 1973/01/27 and want the rest of the values to remain as they are.

Sample Data Date A01
1 1966/05/07 4.870000
2 1966/05/08 4.918333
3 1966/05/09 4.892000
4 1966/05/10 4.858917
5 1966/05/11 4.842000
6 1967/03/18 4.89517
7 1966/05/07 4.870000
8 1966/05/08 4.918333
9 1966/05/09 4.892000
10 2000/05/10 2.858917
11 2001/05/11 1.842000
12 2002/03/18 0.89517

Desired Outcome Date A01
1 1966/05/07 1.4843
2 1966/05/08 1.4990
3 1966/05/09 1.49108 4 1966/05/10 1.480992
5 1966/05/11 1.48565 6 1967/03/18 1.4920
7 1966/05/07 1.4843
8 1966/05/08 1.4991
9 1966/05/09 1.4910
10 2000/05/10 2.858917
11 2001/05/11 1.842000
12 2002/03/18 0.89517

I tried this --->>>
Comb_A01 <- Comb_A01 %>% mutate(A01 = ifelse(Date>= as.Date("1966/05/07", format="%Y/%m/%d") & Date <= as.Date("1973/01/27", format="%Y/%m/%d"), A01 * 0.305, A02))
but it stops multiplying at 1970/10/17

your example data doesnt include examples between 1970 and 1973, so your statement that multiplication stops happening cant be verified on your data.
Also what is A02 and how does it relate? unless A02 is a duplicate of A01, then the function is not leaving the rest of the values as they are, its putting in values from a different column.

Apologies A02 is supposed to be A01, that is a typo.

0

I have a dataset from 1966/05/07 to 2002/05/07, where I am trying to multiply values between 1966/05/07 and 1973/01/27 with 0.305 to change the units of measurement. I am using the code below but it is giving me a warning message and the code stops running in 1971/01/15. What I am doing wrong?

DF <- df %>% mutate(A01 = ifelse(Date>=as.Date("1966/05/07", format="%Y/%m/%d") & Date <= as.Date("1973/01/27", format="%Y/%m/%d"), A01 * 0.305, A01))

Warning messages:

1: In ifelse(Date >= as.Date("1966/05/07", format = "%Y/%m/%d") & Date <=  :
  Incompatible methods ("Ops.factor", ">=.Date") for ">="
2: In ifelse(Date >= as.Date("1966/05/07", format = "%Y/%m/%d") & Date <=  :
  Incompatible methods ("Ops.factor", "<=.Date") for "<="

the implication is that you original Date field, whilest being called date is actually a factor rather than a true Date.

Tank you. I have fixed it using: df <- read.csv("A01.csv", stringsAsFactors = FALSE)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.