1

in my dataset I got a column containing conflict intensity with a rating (1, 2) - now I would like to change this to a Y/N dummie (so instead of intensity rating I want to know if there was a conflict or not in given country in given year) - meaning if there is a NA it should become a 0 if there is any number it should become a 1. The pictures should display it easier :)

I am working with R since 3 days :D so I am very new - I figured out to replicate and rename the column - no I just need to rewrite the content of it, I think the if(){} code might be helpful here - but I dont know what needs to come first in the coding of the function - so I d appreciate help a lot !! thanks :)

this is the original column

and this is the column replicated & renamed - where only the rewriting of the numbers is missing - the red ones are the desired outcome :)

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 3
    (1) Don't post data/code as images (for many reasons, https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/)), just post the data itself (see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info). (2) Welcome to R! This is just `dat$conflict_YN <- +!is.na(dat$conflict_YN)`. – r2evans Aug 12 '21 at 13:54
  • 1
    Please use `dput()` to output your object so we can tell what type it is and more easily use your values – hedgedandlevered Aug 12 '21 at 13:55
  • thanks for the information! makes sense - sry! figured it out - thanks a lot to all :) – Shaggy_JANP Aug 12 '21 at 14:23

2 Answers2

1

In this case df is your dataframe.

df$conflict_YN <- ifelse(is.na(df$conflict_intensity), 0, 1)

Example:

conflict_intensity <- c(1,2,NA)
df <- as.data.frame(conflict_intensity)
df$conflict_YN <- ifelse(is.na(df$conflict_intensity), 0, 1)

Output:

> df
  conflict_intensity conflict_YN
1                  1           1
2                  2           1
3                 NA           0
1

You want to make a new column with something like this

!is.na(dataframe$conflict_YN)

The ! means "not" or "opposite"

is.na() returns a 1 for a value that is NA and a 1 for everything else. So !is.na() will return a 0 for NA and a 1 for everything else.

dataframe is the name I made up for your dataframe or data table.

Have fun with R.

birdy448
  • 11
  • 3