missB
missB

Reputation: 59

create new column based on values on previous rows

I hope somebody can help me.

I have a data like this:             
subject        choice  
 1               3  
 2               3    
 3               1    
 4               4    
 5               3    
 6               2    
 7               2    
 8               3    

now I want to create a new column based on the value of 'choice' column. If the value on choice column is new (has never occurred before), the value on the new column will be 'No', otherwise, if the value has already occur on previous rows , than the value in new column will be 'Soc'. the new table will look like this:

 subject        choice    newcolumn
   1               3           No
   2               3           Soc
   3               1           No
   4               4           No
   5               3           Soc
   6               2           No
   7               2           Soc
   8               3           Soc

can somebody help me? thanks in advance

Upvotes: 0

Views: 1533

Answers (3)

agstudy
agstudy

Reputation: 121568

Another option using duplicated and ifelse:

transform(DF, newcolumn = ifelse(!duplicated(choice),'No','Soc'))

##   subject choice newcolumn
## 1       1      3        No
## 2       2      3       Soc
## 3       3      1        No
## 4       4      4        No
## 5       5      3       Soc
## 6       6      2        No
## 7       7      2       Soc
## 8       8      3       Soc

Upvotes: 1

Gavin Simpson
Gavin Simpson

Reputation: 174788

Using example data

DF <- data.frame(subject = 1:8, choice = c(3, 3, 1, 4, 3, 2, 2, 3))

I would do

DF <- transform(DF, newcolumn = c("No","Soc")[duplicated(choice) + 1])

giving

  subject choice newcolumn
1       1      3        No
2       2      3       Soc
3       3      1        No
4       4      4        No
5       5      3       Soc
6       6      2        No
7       7      2       Soc
8       8      3       Soc

Without transform() this would be

DF$newcolumn <- c("No","Soc")[duplicated(DF$choice) + 1])

Upvotes: 4

Drew Steen
Drew Steen

Reputation: 16607

There are a bunch of ways to do this, but using bracket subsetting will teach you some useful things about R:

# Make your example reproducible
subject <- 1:8
choice <- c(3, 3, 1, 4, 3, 2, 2, 3)
d <- data.frame(subject, choice)

# Create a new column, set all teh values to "No
d$newColumn <- "No"
# Set those values for which choice is duplicated to "Soc"
d$newColumn[duplicated(d$choice)] <- "Soc"

Upvotes: 0

Related Questions