簡體   English   中英

如何在R中使用ifelse函數

[英]How to use ifelse function in r

surveywithoutmissing <- data.frame(Collaboration_A = rep(c(0 "times", 1 "time", 2-4 "times", >4 "times", NA),
                                   "times" = c(0, 20, 26, 11, 118)),
             Collaboration_B = rep(c(0 "times", 1 "time", 2-4 "times",>4 "times", NA),
                                   "times" = c(0, 22, 33, 16, 104)),
             Collaboration_C = rep(c(0 "times", 1 "time", 2-4 "times", >4 "times", NA),
                                   "times" = c(0, 16, 23, 9, 127)),
             Collaboration_D = rep(c(0 "times", 1 "time", 2-4 "times", >4 "times", NA),
                                   "times" = c(0, 24, 12, 4, 135)))

這將無法運行,並且會給出錯誤響應:

Error: unexpected ')' in ""times" = c(0, 24, 12, 4, 135))"

我不確定您要做什么。 但是在我看來,您嘗試用不同的數字重復每個元素(例如,“ 0”,“> 4”)。 也就是說,對於每個協作,您似乎重復“ 0”,“ 1”,“ 2-4”,“> 4”,NA,但重復次數不同。 在這里,我照顧了Collaboration_A和B。

# Set up the number of repetition
ana <- c(0, 20, 26, 11, 118)
bobby <- c(0, 22, 33, 16, 104)

foo <- data.frame(Collaboration_A = rep(c("0", "1", "2-4", ">4", NA), ana),
                  Collaboration_B = rep(c("0", "1", "2-4", ">4", NA), bobby),
                  stringsAsFactors = FALSE)

如果要包括“時間”和“時間”一詞,則需要在引號內。

surveywithoutmissing <- data.frame(Collaboration_A = rep(c("0 times", "1 time", "2-4 times", ">4 times", NA),
                                   "times" = c(0, 20, 26, 11, 118)),
             Collaboration_B = rep(c("0 times", "1 time", "2-4 times", ">4 times", NA),
                                   "times" = c(0, 22, 33, 16, 104)),
             Collaboration_C = rep(c("0 times", "1 time", "2-4 times", ">4 times", NA),
                                   "times" = c(0, 16, 23, 9, 127)),
             Collaboration_D = rep(c("0 times", "1 time", "2-4 times", ">4 times", NA),
                                   "times" = c(0, 24, 12, 4, 135)))

產生

 > surveywithoutmissing
    Collaboration_A Collaboration_B Collaboration_C
1            1 time          1 time          1 time
2            1 time          1 time          1 time
3            1 time          1 time          1 time
4            1 time          1 time          1 time
5            1 time          1 time          1 time
6            1 time          1 time          1 time
7            1 time          1 time          1 time
8            1 time          1 time          1 time
9            1 time          1 time          1 time
10           1 time          1 time          1 time
11           1 time          1 time          1 time
12           1 time          1 time          1 time
13           1 time          1 time          1 time
14           1 time          1 time          1 time
15           1 time          1 time          1 time
16           1 time          1 time          1 time
17           1 time          1 time       2-4 times
18           1 time          1 time       2-4 times
19           1 time          1 time       2-4 times
20           1 time          1 time       2-4 times
21        2-4 times          1 time       2-4 times
22        2-4 times          1 time       2-4 times
23        2-4 times       2-4 times       2-4 times
24        2-4 times       2-4 times       2-4 times
25        2-4 times       2-4 times       2-4 times
26        2-4 times       2-4 times       2-4 times

暫無
暫無

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

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