If-Else语句在R中产生错误的结果

时间:2014-10-06 03:10:22

标签: r if-statement decode

我有一个名为' clean'的数据框。看起来像这样:

structure(list(X = 6:12, CollectorID = c(58302467L, 58302467L, 
57754564L, 58301689L, 58302467L, 58302467L, 57754564L), StartDate = structure(c(7L, 
6L, 5L, 3L, 4L, 2L, 1L), .Label = c("10/5/14 1:49", "10/5/14 2:03", 
"10/5/14 2:06", "10/5/14 2:09", "10/5/14 2:25", "10/5/14 2:48", 
"10/5/14 3:06"), class = "factor")), .Names = c("X", "CollectorID", 
"StartDate"), class = "data.frame", row.names = c(NA, -7L))

我想将CollectorID中的数值转换为名为' Collector'的新字段中的名称。代码我使用

if(clean$CollectorID==58302467){
  clean$Collector<-"Social Media"
} else if(clean$CollectorID==57754564){
    clean$Collector<-"Email"
} else {
  clean$Collector<-"Blog Post"  
}

产生以下警告消息:

In if (clean$CollectorID == 58302467) { :
  the condition has length > 1 and only the first element will be used 

我不理解警告,我的代码也会产生错误的结果;它将clean $ Collector中的所有值设置为&#34; Blog Post&#34;。

2 个答案:

答案 0 :(得分:2)

如果需要,您可以使用ifelse()。 foo是你的数据。

foo$Collector <- ifelse(foo$CollectorID == 58302467, "Social Media",
                      ifelse(foo$CollectorID == 57754564, "Email", "Blog Post"))

#   X CollectorID    StartDate    Collector
#1  6    58302467 10/5/14 3:06 Social Media
#2  7    58302467 10/5/14 2:48 Social Media
#3  8    57754564 10/5/14 2:25        Email
#4  9    58301689 10/5/14 2:06    Blog Post
#5 10    58302467 10/5/14 2:09 Social Media
#6 11    58302467 10/5/14 2:03 Social Media
#7 12    57754564 10/5/14 1:49        Email

鉴于@ KFB的评论,你可以做这样的事情。您使用CollectorID和Desired输出创建另一个数据框(例如,“社交媒体”)。然后,您可以在merge()中使用left_join()dplyr。这是一个merge()

CollectorID <- c(58302467,57754564,58301689)
type <- c("Social Media", "Email", "Blog Post")
ana <- data.frame(CollectorID, type, stringsAsFactors = FALSE)

merge(foo, ana, by = "CollectorID", all = TRUE)

#  CollectorID  X    StartDate         type
#1    57754564  8 10/5/14 2:25        Email
#2    57754564 12 10/5/14 1:49        Email
#3    58301689  9 10/5/14 2:06    Blog Post
#4    58302467  6 10/5/14 3:06 Social Media
#5    58302467  7 10/5/14 2:48 Social Media
#6    58302467 10 10/5/14 2:09 Social Media
#7    58302467 11 10/5/14 2:03 Social Media

答案 1 :(得分:0)

'match'功能也可以在这里使用。使用@ jazzurro的例子:

> ana$type[match(foo$CollectorID, ana$CollectorID)]
[1] "Social Media" "Social Media" "Email"        "Blog Post"    "Social Media" "Social Media" "Email"       
> 
> foo$Collector = ana$type[match(foo$CollectorID, ana$CollectorID)]
> foo
   X CollectorID    StartDate    Collector
1  6    58302467 10/5/14 3:06 Social Media
2  7    58302467 10/5/14 2:48 Social Media
3  8    57754564 10/5/14 2:25        Email
4  9    58301689 10/5/14 2:06    Blog Post
5 10    58302467 10/5/14 2:09 Social Media
6 11    58302467 10/5/14 2:03 Social Media
7 12    57754564 10/5/14 1:49        Email