thewal
thewal

Reputation: 75

r add a new column with conditions from another

I have the following table

Type    Score
B       18
A       23
A       45
B       877
A       654
B       345
A       23445
A       45
A       432
B       22
B       4566
B       2
B       346
A       889

I would like to be able to create a column that takes out the A values, see below

Type    Score   New_Score
B       18      18
A       23      0
A       45      0
B       877     877
A       654     0
B       345     345
A       23445   0
A       45      0
A       432     0
B       22      22
B       4566    4566
B       2       2
B       346     346
A       889     0

I have tried a good few things in r but none of them work for me, any help would be most appreciated.

Upvotes: 0

Views: 52

Answers (5)

akrun
akrun

Reputation: 886928

We can use

 dat$new_score <- ifelse(dat$Type == "B", dat$Score, 0)

Upvotes: 0

TarJae
TarJae

Reputation: 78907

Just for fun. Here is another solution

df$New_Score <- df$Score                             # add New_Score column
df$New_Score1 <- df$New_Score[df$Type == "A"] <- 0   # add 0 with helping column
df = subset(df, select = -(New_Score1))              # remove helping column

Output:

   Type  Score New_Score
 1 B        18        18
 2 A        23         0
 3 A        45         0
 4 B       877       877
 5 A       654         0
 6 B       345       345
 7 A     23445         0
 8 A        45         0
 9 A       432         0
10 B        22        22
11 B      4566      4566
12 B         2         2
13 B       346       346
14 A       889         0

data:

structure(list(Type = c("B", "A", "A", "B", "A", "B", "A", "A", 
"A", "B", "B", "B", "B", "A"), Score = c(18, 23, 45, 877, 654, 
345, 23445, 45, 432, 22, 4566, 2, 346, 889), New_Score = c(18, 
0, 0, 877, 0, 345, 0, 0, 0, 22, 4566, 2, 346, 0)), row.names = c(NA, 
-14L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 0

K.RAC
K.RAC

Reputation: 233

use of dplyr::mutate and case_when should solve the problem, I would think.

library(dplyr)
df <- data.frame(Type=c("B","A","C","D","A","B","A"), Score = c(1,2,3,4,5,6,7))

df_new <- df %>% mutate(New_Score = dplyr::case_when (
                                                        df$Type == "A" ~ as.numeric(0),
                                                         TRUE ~ df$Score
                                                        )#end of case_when
                        )#end of mutate

df_new

Upvotes: 0

AnilGoyal
AnilGoyal

Reputation: 26218

use this

df$New_score <- replace(df$Score, df$Type == 'B', 0)

Check

df <- read.table(text = 'Type    Score
B       18
A       23
A       45
B       877
A       654
B       345
A       23445
A       45
A       432
B       22
B       4566
B       2
B       346
A       889', header = T)

df$New_score <- replace(df$Score, df$Type == 'B', 0)
df
   Type Score New_Score
1     B    18        18
2     A    23         0
3     A    45         0
4     B   877       877
5     A   654         0
6     B   345       345
7     A 23445         0
8     A    45         0
9     A   432         0
10    B    22        22
11    B  4566      4566
12    B     2         2
13    B   346       346
14    A   889         0

Upvotes: 1

jay.sf
jay.sf

Reputation: 72573

Using ifelse.

transform(dat, new_score=ifelse(Type == "B", Score, 0))
#    Type Score new_score
# 1     B    18        18
# 2     A    23         0
# 3     A    45         0
# 4     B   877       877
# 5     A   654         0
# 6     B   345       345
# 7     A 23445         0
# 8     A    45         0
# 9     A   432         0
# 10    B    22        22
# 11    B  4566      4566
# 12    B     2         2
# 13    B   346       346
# 14    A   889         0

Upvotes: 1

Related Questions