Reputation: 17
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":
high_mortality = 1 if mort is greater than or equal to the median for mortality and high_mortality = 0 otherwise
close_equator = 1 if latitude is less than or equal to the median for latitude and close_equator = 0 otherwise
How do I do this? I am a beginner at R.
Upvotes: 0
Views: 101
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
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