簡體   English   中英

r 添加一個新列,其中包含另一個條件

[英]r add a new column with conditions from another

我有下表

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

我希望能夠創建一個取出 A 值的列,見下文

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

我在 r 中嘗試了一些很好的東西,但它們都不適合我,任何幫助將不勝感激。

使用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

用這個

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

查看

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

我認為使用 dplyr::mutate 和 case_when 應該可以解決問題。

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

純娛樂。 這是另一個解決方案

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

數據:

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"))

我們可以用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM