2

Say I have a data set

car_manu    owner
ford         1
toyota       1
ford         2
ford         3
ford         3
ford         3

I'd like to make a variable that says if they're a 'one car owner' - this would mean owner 2 is one car owner. I know this would use the unique(df$owner) but I'm not sure to assign this to an observation.

The expected output is

 car_manu owner     type
     ford     1 multicar   
   toyota     1 multicar
     ford     2   onecar
     ford     3 multicar
     ford     3 multicar
     ford     3 multicar
akrun
  • 874,273
  • 37
  • 540
  • 662
PDog
  • 143
  • 1
  • 2
  • 11

5 Answers5

3

Here's a base R possibility

tab <- tabulate(df$owner)
cbind(df, type = rep(ifelse(tab == 1L, "onecar", "multicar"), tab))
#   car_manu owner     type
# 1     ford     1 multicar
# 2   toyota     1 multicar
# 3     ford     2   onecar
# 4     ford     3 multicar
# 5     ford     3 multicar
# 6     ford     3 multicar

where

df <- structure(list(car_manu = structure(c(1L, 2L, 1L, 1L, 1L, 1L), .Label = c("ford", 
"toyota"), class = "factor"), owner = c(1L, 1L, 2L, 3L, 3L, 3L
)), .Names = c("car_manu", "owner"), class = "data.frame", row.names = c(NA, 
-6L))
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
2

using dplyr:

library(dplyr)
df %>% group_by(owner) %>%
       mutate(onecar = ifelse(n()==1, 1, 0))
jeremycg
  • 24,657
  • 5
  • 63
  • 74
1

If you need to create a column based on the number of observations using 'owner' as a grouping variable, then we could use data.table. We change the 'data.frame' to 'data.table' (setDT(df1)). Grouped by 'owner', we check the condition if(.N==1) it will be 'one car owner' or else 'multi-car' and assign the output to a new column 'owner_type'

 library(data.table)
 setDT(df1)[, owner_type :=if(.N==1) 'one-car' else 'multi-car', owner]

Or without using the if/else condition, we can create a logical vector (.N!=1) by 'owner', add 1 to it and use that as numeric index to replace that with 'one-car', 'multi-car'.

  setDT(df1)[, owner_type:=c('one-car', 'multi-car')[(.N!=1)+1] , owner]
  df1
  #   car_manu owner owner_type
  #1:     ford     1  multi-car
  #2:   toyota     1  multi-car
  #3:     ford     2    one-car
  #4:     ford     3  multi-car
  #5:     ford     3  multi-car
  #6:     ford     3  multi-car
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Using base and given the dataframe called d you can use an ifelse and the ave function as shown below:

d$type=ifelse(ave(d$owner,d$owner,FUN=length)==1,"one car","multicar")
d
  #   car_manu owner type
  #1:     ford     1  multi-car
  #2:   toyota     1  multi-car
  #3:     ford     2    one-car
  #4:     ford     3  multi-car
  #5:     ford     3  multi-car
  #6:     ford     3  multi-car
User7598
  • 1,658
  • 1
  • 15
  • 28
0

In simple base R

d$type<-apply(d,1,function(x){length(which(d$owner==x[2]))})

As I am in process of learning R, this might not be one of the most efficient methods but is easy to use and understand.

Saksham
  • 9,037
  • 7
  • 45
  • 73