Home > Blockchain >  How do I create a new variable from specific variables?
How do I create a new variable from specific variables?

Time:02-20

Noob question, but how would I create a separate variable that is formed from specific attributes of other variables? For example, I'm trying to find Asian countries in the "region" variable that have a "democracy" variable score of "3." I want to create a variable called "asia3" that selects those Asian countries with a democracy score of 3.

CodePudding user response:

The which operator should solve your request.

asia3 <- your_data[ which(your_data$Region=='Asia' & your_data$democracy == 3), ]

CodePudding user response:

In base R, you can create a new variable based on a condition using an ifelse statement, then assign to a new variable called asia3.

df$asia3 <- ifelse(df$region == "Asia" & df$democracy == 3, "yes", "no")

     region democracy asia3
1      Asia         3   yes
2 Australia         3    no
3      Asia         2    no
4    Europe         1    no

Or if you only need a logical output, then you do not need the ifelse:

df$asia3 <- df$region == "Asia" & df$democracy == 3

     region democracy asia3
1      Asia         3  TRUE
2 Australia         3 FALSE
3      Asia         2 FALSE
4    Europe         1 FALSE

or with tidyverse

library(tidyverse)

df %>%
    mutate(asia3 = ifelse(df$region == "Asia" & df$democracy == 3, TRUE, FALSE))

However, if you only want to keep the rows that meet those conditions, then you can:

#dplyr
df %>%
    filter(region == "Asia" & democracy == 3)

#base R
df[df$region=='Asia' & df$democracy == 3, ]

#  region democracy
#1   Asia         3

Data

  df <-
    structure(list(
      region = c("Asia", "Australia", "Asia", "Europe"),
      democracy = c(3, 3, 2, 1)
    ),
    class = "data.frame",
    row.names = c(NA,-4L))
  •  Tags:  
  • r
  • Related