Converting numeric vector to binary

Hi all,
I'm just starting with RStudio so bear with me please!
I have a vector of numeric variables consisting of daily rainfall data, and I need to convert it to binary as in: 0mm = 0, anything other than 0 = 1. Any suggestions how to do this?
Many thanks!

x <- c(0,0, rnorm(2), 0, 0, rnorm(2))

y <- ifelse(x == 0, 0, 1)

So this returned a value list of 8 observations instead of the original 972 that I had and need...any ideas?

I defined an x variable as an example. Are you perhaps mistakenly feeding that to the function?

I guessed that so i wrote this:

df_full$rainfall <- c(0,0, rnorm(2), 0, 0, rnorm(2))

Rainfall <- ifelse(x == 0, 0, 1)

I'm sorry, I'm a little confused now! Haha

I suppose you have a dataframe df_full, where you have one variable you wish to convert to binary?

What is the name of that variable in df_full?

1 Like

Hi @costanza, could you post part of your table, df_full? To do this, first run dput(head(df_full, 50)), and then paste the output here, between a pair of triple backticks (```), like this:

```
<-- paste output of dput(head(df_ful, 50)) here
```
1 Like

the rnorm stuff is to invent made up data, to serve as an example. for your own data with name df_full$rainfall, to make the vector you want called Rainfall do

Rainfall <- ifelse(df_full$rainfall  == 0, 0, 1)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.