清理人口普查局的数据框架

时间:2015-02-12 22:46:22

标签: r performance for-loop dataframe

我正在努力清理和操纵人口普查局数据的数据框架。我在R中使用(for loop)来做到这一点,但到目前为止这需要超过20个小时!!

问题是我使用了两个不同的数据框

这是我的代码

t=1
for(i in 1:25558){ # number of records in the Housing record

family <- array(0,dim=c(0,12)) # creating an empty array to store row number

k=1
n=0
for(j in t:52608){  # number of records in the Personal record
   if(Housing[i,5] == Personal[j,2]) {
     family[k]=j
     k=k+1
     n=1
     }
   else( 
    if(n == 1) {
      t=j
      break
     }
    )
}

a=0 
for(m in 1:length(family)){
    if(is.na(Personal[family[m],22])) {  # Some families has mix values: NA and numbers
      break
      }

 else( 
     if(Personal[family[m],22] > 1){
     a=a+1
     }
     )
  }

 if(a == length(family)) {
     Housing[i,1]=1
    }

}

(编辑 - 一个例子): 在Hosing记录中,我有一个每个家庭的ID。在个人记录中,所有家庭成员都会重复使用相同的家庭ID。

Housing Record:
ID  Family Ability to Speak English
1             0
2             0
3             1



Personal Record:
ID  Member  Person Ability to Speak English
1     1                    1
1     2                    NA
1     3                    2
2     1                    4
2     2                    1
3     1                    3
3     2                    2 

注意:&#34; NA&#34;这里并不意味着&#34;不可用&#34;,它具有特定的含义(基本上我不应该删除它)

我需要更改列的值&#34;家庭能力说英语&#34;基于家庭成员说英语的能力。 (参见我的代码的最后一部分)

1 个答案:

答案 0 :(得分:0)

# some dummy data frame for families
family <- data.frame(famid=rep(1:5, each=3), 
                     member=rep(1:3, 5), 
                     speake=sample(c(1:4, NA, NA), 15, replace=TRUE))

# a function to calculate scores 
# (modify according to your scoring algorithm)
english_score <- function(fam){
    # pull out the English scores for all members of fam
    data <- family$speake[which(family$famid==fam)]
    # I dont know how you want to number the families,
    # by their combined English score, or just if any exist
    # so demonstrate both
    eng_sum <- sum(na.omit(data))
    eng_present <- !any(is.na(data))

    #return this result of the function as a vector
    c(fam, eng_sum, eng_present)
}

# apply the function to each unique family
housing <- sapply(unique(family$famid), english_score)
housing <- as.data.frame(t(housing))
colnames(housing) <- c("family", "eng_sum", "eng_present")