如何在for循环中绑定表

时间:2015-12-01 18:16:10

标签: r rbind

我正在尝试创建一个汇总数据集数据的表。我有:

set.seed(123) 
age <- runif(100, 1, 100)
gender <- sample(c("Male", "Female"), 100, replace=TRUE)
bmi <- rep(c("Normal"), 100)
height <- runif(100, 150, 190)
smoker <- sample(c("Yes", "No"), 100, replace=TRUE)

finaldata <- data.frame(age, gender, bmi, height, smoker)
str(finaldata)
continuous <- finaldata[ ,c(1, 4)]
categorical <- finaldata[ ,c(2, 3, 5)]


Table1 <- function(CONT, CAT, DIGITS=2){
table_cont <- matrix(0, ncol=2, nrow=ncol(CONT))
for (i in 1:ncol(CONT)){
table_cont[i, ] <- c(round(mean(CONT[ ,i]), DIGITS), round(sd(CONT[ ,i]), DIGITS))
}

cats <- function(VARIABLE){
table_cat <- matrix(0, ncol=2, nrow=dim(table(CAT[ ,VARIABLE])))
for (i in 1:dim(table(CAT[ ,VARIABLE]))){
table_cat[i, ] <- c(table(CAT[ ,VARIABLE])[i], paste(round(prop.table(table(CAT[ ,VARIABLE]))[i]*100, DIGITS), "%"))
}
rownames(table_cat) <- levels(CAT[, VARIABLE])
table_cat <- rbind(rep("", ncol=ncol(table_cat)), table_cat)
return(table_cat)
}
table_cat <- rbind(cats(1), cats(2), cats(3))

descriptives <- rbind(table_cont, table_cat)
return(descriptives)
}
Table1(continuous, categorical)

工作正常。也就是说,为了绑定分类变量,我正在做rbind(猫(1),猫(2),猫(3))。虽然这对于这个数据集是可以的,但我不想继续改变我使用的每个其他数据集。我尝试在for循环中绑定它们但是没有成功。如何在没有重复指定rbind(猫(1),猫(2),猫(3))的情况下如何绑定它们?

3 个答案:

答案 0 :(得分:2)

试试这个:

table_cat <- data.frame()
# N here is the number of cat() function calls you plan on making
for(i in 1:N){
   table_cat <- rbind(table_cat,cat(i))
}

如果你不想要rownames问题,试试这个:

table_cat <- matrix(nrow=0,ncol=ncol(cats(1)))
  for(i in 1:3){
    table_cat <- rbind(table_cat,cats(i))
  }

答案 1 :(得分:0)

除非您的行相互依赖,否则您应使用applyplyr&#39; s ddply等函数来处理数据而不使用所有for循环。

cont.func <- function(CONT.col, DIGITS=2){
     c(round(mean(CONT.col), DIGITS), round(sd(CONT.col), DIGITS))
}
CONT = t(apply(continuous,2,cont.func))
cat.func <- function(CAT.col,DIGITS=2){
tab = table(CAT.col)
rbind(cbind(tab, paste0(round(prop.table(tab)*100, DIGITS), "%")),"")
}
CAT = do.call("rbind",apply(categorical,2,cat.func))
rbind(CONT,c("",""),CAT)

此外,您可以在as.data.frame rbind调用cat.func周围使用<?php session_start(); include_once 'config.php'; $email =""; $userpassword =""; $errors = 0; $emailError =""; $passwordError =""; if(isset($_SESSION['user'])!="") { header("Location: home.php"); } if(isset($_POST['loginBtn'])) { if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) { $emailError = "Email is not valid"; $errors = 1; } if(!empty($_POST["password"])) { $passwordError = "Please enter a Password"; $errors = 1; } $email = mysql_real_escape_string($_POST['email']); $userpassword = mysql_real_escape_string($_POST['password']); $result=mysql_query("SELECT * FROM users WHERE emailAddress='$email'"); $row=mysql_fetch_array($result); if($row['password']==md5($userpassword)) { $_SESSION['user'] = $row['user_id']; header("Location: home.php"); } else { ?> <script>alert('First time visitors, please create an account to play'); </script> <?php } } ?> 来保留创建CAT时的分类变量名称。根据您的需要,这可能比使用空白报价更可取。

答案 2 :(得分:0)

你想这样做:

library(dplyr)
library(tidyr)

better_summary = function(data){
  continuous = data %>% Filter(is.numeric, .)
  categorical = data %>% Filter(. %>% is.numeric %>% `!`, .)

  continuous_summary = 
    continuous %>%
    gather(variable, value) %>%
    group_by(variable) %>%
    summarize(mean = mean(value),
              sd = sd(value))

  categorical_summary = 
    categorical %>%
    gather(variable, value) %>%
    count(variable, value) %>%
    mutate(percent = n / sum(n))

  list(continuous_summary = continuous_summary,
       categorical_summary = categorical_summary)
}