trying to keep it as simple as possible
Consider this simple excel formula. Lets presume that I'm in cell C2 currently and it holds this formula. =if(A2=1,B2,C1)
i'm stuck at the referencing part. is there any way to do it?
trying to keep it as simple as possible
Consider this simple excel formula. Lets presume that I'm in cell C2 currently and it holds this formula. =if(A2=1,B2,C1)
i'm stuck at the referencing part. is there any way to do it?
You have to remember that R is not a spreadsheet such as Excel. Therefor, R does not refer to cell ID's in the same way. However, the following code reproduces your situation:
library(dplyr)
example_data = data.frame(A = sample(c(0, 1), 10, replace = TRUE), B = 6:15, C = 21:30)
example_data %>% mutate(new_column = ifelse(A == 1, B, c(NA, C[1:(length(C) - 1)])))
Here, example_data
is a so called data.frame
, which is equivalent to the contents of the spreadsheet. In the second line we create a new column which uses the same logic as the Excel formula you provided.
Have a look at the different things going on here, try and understand what happens. If you get stuck, I would recommend you read up on some R tutorials (for example) and come back to this example.