I have a dataframe as below:
data <- data.table (x= c(1,2,3,4,5), y= c(7,6,8,9,10), z= c('a','b','c','d','e'), w=c(01.05, 03.04, 11.08, 05.07, 09.18))
I need to make a new column containing values as I describe below: whenever x equals 3 or 5, or y equals 7, then make a new column called M and insert the value in the corresponding row from w into column M, and leave the other rows empty. The data shuld then look like this:
data <- data.table (x= c(1,2,3,4,5), y= c(7, 6,8,9,10), z= c('a','b','c','d','e'), w=c(01.05, 03.04, 11.08, 05.07, 09,18), M=c(01.05, NA, 11.08, NA, 09.18)
I am a beginner in R and searched a lot but could not find a solution. I tried using case_when and apply functions but no luck. I would really appreciate it if anyone can help me with this.