0

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 1967 to 1973and 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        
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Using `dplyr`: `df %>% mutate(A01 = ifelse(year(Date) >= 1967 & year(Date) <= 1973, A01 * 0.305, A01))` – alex_jwb90 Sep 19 '20 at 23:13
  • 1
    `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))` - should actually even work without the parsing, but better to be clear. If your "Date"-column isn't yet a Date type and you run into any issues because of that, you can parse it the same way using `as.Date` or functions from the great `lubridate` package – alex_jwb90 Sep 19 '20 at 23:32
  • Based on the data that you have shared only row 6 should be changed since it is in the year 1967. Why others row change? – Ronak Shah Sep 20 '20 at 01:36
  • The units of the values to change are in Feet and I want it in Metres. The rest of the data is already in metres – Lindiwe nkaBANE Sep 20 '20 at 10:34
  • @alex_jwb90, why does this code stop multiplying @ 1970/10/17 – Lindiwe nkaBANE Sep 20 '20 at 20:19
  • No way to tell. It shouldn't. Is your column "Date" a Date type vector for sure? Can you `dput` your dataframe to let us reproduce this behavior? – alex_jwb90 Sep 20 '20 at 20:26
  • needed to read the date column before using the df. I used: df <- read.csv("A01.csv", stringsAsFactors = FALSE), to fix to issue – Lindiwe nkaBANE Sep 21 '20 at 13:18

3 Answers3

2

An option in base R would be get the 'year' part from the Date column to create a logical index ('i1'), subset the 'A01' column, multiply by 0.305 and assign it back to the original column

i1 <- as.numeric(format(as.Date(df1$Date, '%Y/%m/%d'), "%Y")) %in% 1966:1973
df1$A01[i1] <- df1$A01[i1] * 0.305

data

df1 <- structure(list(Date = c("1966/05/07", "1966/05/08", "1966/05/09", 
"1966/05/10", "1966/05/11", "1967/03/18", "1966/05/07", "1966/05/08", 
"1966/05/09", "2000/05/10", "2001/05/11", "2002/03/18"), A01 = c(4.87, 
4.918333, 4.892, 4.858917, 4.842, 4.89517, 4.87, 4.918333, 4.892, 
2.858917, 1.842, 0.89517)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Convert Date to date class, extract the year from it and multiply A01 with 0.305 if it is between 1967 and 1974 or with 1 otherwise.

library(dplyr)
library(lubridate)

df %>%
  mutate(Date = ymd(Date), 
         A01 = A01 * c(1, 0.305)[(between(year(Date), 1967, 1974)) + 1])
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

Another base R option using ifelse

transform(
  df,
  A01 = A01 * ifelse(as.numeric(format(as.Date(Date), "%Y")) %in% 1967:1973, 0.305, 1)
)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81