maddie
maddie

Reputation: 27

problem with mutating a new column using values of another column

I am a very new R user and have a problem with mutating a new variable for the dataset below. I would like to create a new numeric column and assign value 1 if company!=NA, while assign value 2 if company==NA. Then, I would like to assign the label "Yes" to 1s and "No" otherwise.

Thanks,

MRR

    fy <- c(2010,2011,2012,2010,2011,2012,2010,2011,2012)
company <- c(NA,NA,"Apple","Google","Google","Google","Microsoft","Microsoft","Microsoft")
revenue <- c(65225,108249,156508,29321,37905,50175,62484,69943,73723)
profit <- c(14013,25922,41733,8505,9737,10737,18760,23150,16978) 
companiesData <- data.frame(fy, company, revenue, profit)

Upvotes: 1

Views: 37

Answers (3)

R.W.
R.W.

Reputation: 99

Here's a case_when() solution.

library(tidyverse)

companies <- tibble(
  fy = c(2010,2011,2012,2010,2011,2012,2010,2011,2012),
  company = c(NA,NA,"Apple","Google","Google","Google","Microsoft","Microsoft","Microsoft"),
  revenue = c(65225,108249,156508,29321,37905,50175,62484,69943,73723),
  profit = c(14013,25922,41733,8505,9737,10737,18760,23150,16978) 
)

companies %>% 
  mutate(missing_value = case_when(is.na(company) == TRUE ~ 2, 
                                   !is.na(company) == TRUE ~ 1),
         missing_label = case_when(missing_value == 1 ~ "Yes",
                                   missing_value == 2 ~ "No"))
#> # A tibble: 9 × 6
#>      fy company   revenue profit missing_value missing_label
#>   <dbl> <chr>       <dbl>  <dbl>         <dbl> <chr>        
#> 1  2010 <NA>        65225  14013             2 No           
#> 2  2011 <NA>       108249  25922             2 No           
#> 3  2012 Apple      156508  41733             1 Yes          
#> 4  2010 Google      29321   8505             1 Yes          
#> 5  2011 Google      37905   9737             1 Yes          
#> 6  2012 Google      50175  10737             1 Yes          
#> 7  2010 Microsoft   62484  18760             1 Yes          
#> 8  2011 Microsoft   69943  23150             1 Yes          
#> 9  2012 Microsoft   73723  16978             1 Yes

Created on 2021-10-11 by the reprex package (v2.0.1)

Upvotes: 1

akrun
akrun

Reputation: 887048

We may use base R

companiesData$column1 <- with(companiesData, c("No", "Yes")[1 + !is.na(company)])

Upvotes: 1

useR_3001
useR_3001

Reputation: 36

fy <- c(2010,2011,2012,2010,2011,2012,2010,2011,2012)
company <- c(NA,NA,"Apple","Google","Google","Google","Microsoft","Microsoft","Microsoft")
revenue <- c(65225,108249,156508,29321,37905,50175,62484,69943,73723)
profit <- c(14013,25922,41733,8505,9737,10737,18760,23150,16978) 
companiesData <- data.frame(fy, company, revenue, profit)

companiesData <- companiesData %>%
  mutate(column1 = ifelse(!is.na(company), 1, 0),
         column2 = ifelse(!is.na(company), "Yes", "No"))

Upvotes: 1

Related Questions