使用基于另一列

时间:2015-09-24 19:51:21

标签: r

我有一个名为省的列的数据框,我需要添加一个名为Region的新列。该值基于省列。这是数据框:

    Province 
 1  Alberta   
 2  Manitoba
 3  Ontario
 4  British Columbia
 5  Nova Scotia
 6  New Brunswick
 7  Quebec

输出:

   Province            Region
 1  Alberta             Prairies
 2  Manitoba            Prairies
 3  Ontario             Central
 4  British Columbia    Pacific
 5  Nova Scotia         East
 6  New Brunswick       East
 7  Quebec              East

我在R中尝试了这段代码但它无法正常工作。

Region <- as.character(Province) 

if (length(grep("British Comlumbia", Province)) > 0) { 
    return("Pacific") 
}

1 个答案:

答案 0 :(得分:0)

您可以创建向量并逐步替换。这可能不是一种合适的方式,但这可行。

Prairies <- c("Alberta","Manitoba")
Central <- c("Ontario")
Pacific <- c("British Colombia")
East <- c("Nova Scotia","New Brusnwick","Quebec")
#make a copy of the column province
df$Region <- as.vector(df[,1])
#one by one replace the items based on your vectors
df$Region <- replace(df$Region, df$Region%in%Prairies, "Prairies")
df$Region <- replace(df$Region, df$Region%in%Central, "Central")
df$Region <- replace(df$Region, df$Region%in%Pacific, "Pacific")
df$Region <- replace(df$Region, df$Region%in%East, "East")