jo.magi jo.magi - 1 year ago 96
R Question

Creating new columns

I have a column that specifies the type os sanctions used in my data. This is what it looks like:

country sanction_type
(chr) (int)
1 China 2
2 Austria 5
3 South Africa 1
4 Poland 6
5 Poland 7
6 Bolivia, Plurinational State of 2

The types of sanctions range from 1-10. How can I create two extra columns, one including sanction types 1,2,3,4 and the other one 5,6,7,8,9,10. I would also like to keep the exisiting one with all sanctions types. Many thanks!

The dataset has more than 6 observations, this is just a sample of the data. Sorry for the confusion.

Answer Source

Using dplyr package:

country <- c("China","Austria","South Africa","Poland", "Poland", "Bolivia")
sanction_type <- c(2,5,1,6,7,2)
df <- data.frame(country, sanction_type)


df <- mutate(df, srange1 = ifelse(sanction_type <= 4, 1, 0),
             srange2 = ifelse(sanction_type >= 5, 1, 0))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download