-2

I am trying to translate SAS code below:

data year5 year10;
set loans; 
if ID in ("ELC", "OEC") then output year5;
else output year10;
run;

How can I do this in R so that if a value is in one set of values, it will output to one dataframe, but if it's not it will output to another?

More specifically, there are three data frames I'm working with. The original data frame, which contains the column 'ID'. If Column 'ID' has the value "ELC" or "OEC" in it, then those rows from the Original data frame will be outputted to the new dataframe Year5, else (there is another value in that 'ID' column) those rows (from Original dataframe) will be outputted to the new dataframe year10. I already have empty dataframes created for Year5 and Year10.

Leslie
  • 1
  • 1
  • 4
    Look at the base R function `split`. To give much more help, you'll need to post a [reproducible question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – camille Aug 14 '18 at 14:31
  • 1
    Hello, you need to better formulate the question, post an example of in and out, so we can understand what you really need. If possible even a sample of the data set. – Thiago Fernandes Aug 14 '18 at 14:33
  • I updated to clarify a little more, hope this helps more and thank you for your help! – Leslie Aug 14 '18 at 14:55
  • Leslie, I'm not familiar with SAS enough to be able to "know" that these represent multiple different frames. Instead of describing the three frames, please just *make* some, perhaps 3-4 rows each, with `data.frame(ID=c("ELC","OEC","OEC","OEC"),...)`, etc. And then please be clear with your intended output. – r2evans Aug 14 '18 at 15:13

2 Answers2

1

If you need two separate dataframes I would just filter (or use subset) on the dataframe and make two new ones using dplyr. (Not the most efficient I guess)

library(dplyr)

year5  <- filter(df, ID %in% c("ELC","OEC"))
year10 <- filter(df, !ID %in% c("ELC","OEC"))
R. Prost
  • 1,958
  • 1
  • 16
  • 21
0

If I understand your question correctly, you need an ifelse statement

set_of_values <- c("ELC", "OEC") df$output <- ifelse(df$ID %in% set_of_values, df$year5, df$year10)

Here I am assuming that ID, year5, year10 are columns in your dataframe df

SmitM
  • 1,366
  • 1
  • 8
  • 14
  • year5 and year 10 are two separate data frames. ID comes from the original dataframe. So, in total there are 3 data frames. One: Original, Two: Year5, Three: Year10. I am trying to filter the original dataframe so that if those values (ELC and OEC) are in the column ID, then they would be output in the dataframe year5, else (ID not equal to those two) they would be output in year10. – Leslie Aug 14 '18 at 14:51
  • In that case, you are better off with a subset() function....`year5 <- subset(original_df, ID %in% set_of_values)` `year10 <- subset(original_df, !ID %in% set_of_values)` – SmitM Aug 14 '18 at 15:13