Inoxout
Inoxout

Reputation: 17

How to create new variables in R?

I have a data set with these variables:

name, gdp, mort, latitude

        name        gdp       mort latitude
1    Algeria  4402.8190  78.200000   0.3111
2     Angola  2368.4710 280.000000   0.1367
3  Argentina  9228.0230  68.899990   0.3778
4  Australia 19930.3600   8.549999   0.3000
5    Bahamas 10829.1800  85.000000   0.2683
6 Bangladesh   972.6265  71.410000   0.2667

Data :

data <- structure(list(name = c("Algeria", "Angola", "Argentina", 
    "Australia", 
    "Bahamas", "Bangladesh"), gdp = c(4402.819, 2368.471, 9228.023, 
    19930.36, 10829.18, 972.6265), mort = c(78.2, 280, 68.89999, 
    8.549999, 85, 71.41), latitude = c(0.3111, 0.1367, 0.3778, 0.3, 
    0.2683, 0.2667)), row.names = c(NA, 6L), class = "data.frame")

The question is :

Construct two new variables based on "mort" and "latitude":

How do I do this? I am a beginner at R.

Upvotes: 0

Views: 101

Answers (2)

Chriss Paul
Chriss Paul

Reputation: 1101

A data.table the solution, one of the multiple advantages is that you don't need $ to call your variable. ?data.table to further details about this package.

library(data.table)
setDT(data)
data[, high_mortality := ifelse(mort >= median(mort), 1, 0)]
data[, close_equator := ifelse(latitude <= median(latitude), 1, 0)]
data

         name        gdp       mort latitude high_mortality close_equator
1:    Algeria  4402.8190  78.200000   0.3111              1             0
2:     Angola  2368.4710 280.000000   0.1367              1             1
3:  Argentina  9228.0230  68.899990   0.3778              0             0
4:  Australia 19930.3600   8.549999   0.3000              0             0
5:    Bahamas 10829.1800  85.000000   0.2683              1             1
6: Bangladesh   972.6265  71.410000   0.2667              0             1

Upvotes: 1

Waldi
Waldi

Reputation: 41260

With base R:

data$high_mortality <- as.numeric(data$mor >= median(data$mort))
data$close_equator  <- as.numeric(data$latitude <= median(data$latitude))

With dplyr:

library(dplyr)

data %>% mutate(high_mortality = as.numeric(mort >= median(mort)),
              close_equator = as.numeric(latitude <= median(latitude)))

        name        gdp       mort latitude high_mortality close_equator
1    Algeria  4402.8190  78.200000   0.3111              1             0
2     Angola  2368.4710 280.000000   0.1367              1             1
3  Argentina  9228.0230  68.899990   0.3778              0             0
4  Australia 19930.3600   8.549999   0.3000              0             0
5    Bahamas 10829.1800  85.000000   0.2683              1             1
6 Bangladesh   972.6265  71.410000   0.2667              0             1

Upvotes: 2

Related Questions