基于同一R数据帧中的另一个向量将观察值分配给组

时间:2016-06-11 17:38:57

标签: r for-loop dataframe

我试图根据网格方形ID为R中的数据框中的观察值分配区域。我有以下数据框( df ):

    for(i in 1:length(df$square))  {
    for(j in 1:length(N)) {
    if(df$square[i]==N[j]){
    df$area[i]=="N"}
    }
    }

    for(i in 1:length(df$square))  {
    if(any(df$square==N)==T){
    df$area[i]=="North"}
    }

我想为"区域"添加另一个列,将每个观察分配到" North"," East"," South& #34;或" West"基于网格方块。我已经尝试了以下 for 循环,但没有做任何事情,

    N <- c("A1","A2","B2")

其中&#34; N&#34;是我创建的一个对象,包含位于北方的方块,即:

{{1}}

我确实找到了以下相关问题,但我想知道在涉及角色时它是否有所不同:Assign a group number based on another column by group in R

任何帮助将不胜感激。感谢

3 个答案:

答案 0 :(得分:1)

我建议使用第二个N配对方块而不是像data.frame那样定义向量:

df <- data.frame(year = 2000,
                 month = c(2,2,2,2,2,3,3,3),
                 square = c("A1", "B2", "H5", "J9", "A2", "N8", "M9", "C7"),
                 stringsAsFactors = FALSE)
areas <- data.frame(square = c("A1", "A2", "B1", "H5", "J9", "M9", "N8"),
                    area = c("N", "N", "N", "W", "E", "S", "S"),
                    stringsAsFactors = FALSE)

有了它,只需进行合并:

merge(df, areas, by = "square", all.x = TRUE)
#   square year month area
# 1     A1 2000     2    N
# 2     A2 2000     2    N
# 3     B2 2000     2 <NA>
# 4     C7 2000     3 <NA>
# 5     H5 2000     2    W
# 6     J9 2000     2    E
# 7     M9 2000     3    S
# 8     N8 2000     3    S

NA是因为areas定义不完整。)

答案 1 :(得分:0)

d <- data.frame(year = rep(2000, 8), month = rep(3,8),
            square = c("A1", "B2", "H5", "J9", "A2", "N8", "M9", "C7"))

N <- c("A1","A2","B2")

for(i in 1:nrow(d))  {
    if (d$square[i] %in% N) {
        d$area[i] <- "North"
    }
    else (
        d$area[i] <- "Somewhere Else"
    )
}

用于其他基数方向id向量的for循环中的else if()语句中的图层

答案 2 :(得分:0)

在R中,通常最好避免循环,尤其是嵌套循环。对于这种情况,我更喜欢sapply()

N <- c("A1","A2","B2")
#assume these are the other designations
S <- c("H5", "J9")
E <- c("N8","M9")
W <- c("C7")

mydat$area<- sapply(mydat$square, function (x){
  if (x %in% N)  return("North")
  if (x %in% S)  return("South")
  if (x %in% E)  return("East")
  if (x %in% W)  return("West")
  else NA
  }) 
mydat

year month square  area
2000     2     A1 North
2000     2     B2 North
2000     2     H5 South
2000     2     J9 South
2000     2     A2 North
2000     3     N8  East
2000     3     M9  East
2000     3     C7  West

当你开始拥有大型数据集时,*apply()函数将比R中的循环快得多。