0

Can somebody please explain something about data frames to me in R, please?

For example, I have the following SPSS code:

if (age=33 or age=34 or age=35) agegrp3335=1
EXECUTE.

if (age=33) age33=1.
EXECUTE.

if (age=34) age34=1.
EXECUTE.

if (age=35) age353=1.
EXECUTE.

Hadley Wickham's advice, is the following:

x <- if (y < 20) "Too low" else "Too high"

But isn't this creating a new dataframe 'x'?

Any suggestions on creating a new variable on an existing R dataframe, please?

Many thanks.

David Nichols
  • 576
  • 3
  • 5
  • 1
    You can add a new column (that uses values from an existing column) to an existing data.frame by using `df$new_column <- ifelse(df$y < 20, "Too low", "Too high")` – dario Feb 19 '20 at 15:30
  • 1
    `x` will be a character vector of length 1. A data frame is like a table having columns and rows – DeltaKappa Feb 19 '20 at 15:33
  • Ahh thanks. That's what I was thinking but wasn't too sure... – John Wildman Feb 19 '20 at 15:35
  • How's this for the first line then? 2019data$agegroup3335 <- ifelse((2019data$age==33) | (2019data$age==34) | (2019data$age==35), 1) – John Wildman Feb 19 '20 at 15:40
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 19 '20 at 16:14

0 Answers0