如何cbind和rbind 2个数据帧列表

时间:2017-12-24 00:33:08

标签: r rbind cbind

我有2个数据帧列表。每个列表都有24个数据帧

list1 <- (data1, data2, ..., data24)
list2 <- (result1, result2, ...., result3)

我希望cbind data1result1data2result2等等。然后rbin将所有数据框组合在一起:

all1 <- cbind(data1, result1)
all2 <- cbind(data2, result2)
all.in <- rbind(all1, all2)

如何有效地使用24个数据帧?

2 个答案:

答案 0 :(得分:6)

在tidyverse语法中,<?php session_start(); // variable declaration $username = ""; $errors = array(); $_SESSION['success'] = ""; // connect to database $db = mysqli_connect('localhost', 'root', '', 'registration'); // REGISTER USER if (isset($_POST['reg_user'])) { // receive all input values from the form $username = mysqli_real_escape_string($db, $_POST['username']); $password_1 = mysqli_real_escape_string($db, $_POST['password_1']); $password_2 = mysqli_real_escape_string($db, $_POST['password_2']); // form validation: ensure that the form is correctly filled if (empty($username)) { array_push($errors, "Username is required"); } if (empty($password_1)) { array_push($errors, "Password is required"); } if ($password_1 != $password_2) { array_push($errors, "The two passwords do not match"); } // register user if there are no errors in the form if (count($errors) == 0) { $password = md5($password_1);//encrypt the password before saving in the database $query = "INSERT INTO users (username, password) VALUES('$username', '$password')"; mysqli_query($db, $query); $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); } } // LOGIN USER if (isset($_POST['login_user'])) { $username = mysqli_real_escape_string($db, $_POST['username']); $password = mysqli_real_escape_string($db, $_POST['password']); if (empty($username)) { array_push($errors, "Username is required"); } if (empty($password)) { array_push($errors, "Password is required"); } if (count($errors) == 0) { $password = md5($password); $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) { $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); }else { array_push($errors, "Wrong username/password combination"); } } } ?> dplyr::bind_rows会将列表绑定在一起,并由bind_cols变体调用:

purrr::map_df

library(tidyverse) l1 <- list(mtcars[1:2, 1, drop = FALSE], mtcars[3:4, 1, drop = FALSE]) l2 <- list(mtcars[1:2, 2:6], mtcars[3:4, 2:6]) map2_dfr(l1, l2, bind_cols) #> mpg cyl disp hp drat wt #> 1 21.0 6 160 110 3.90 2.620 #> 2 21.0 6 160 110 3.90 2.875 #> 3 22.8 4 108 93 3.85 2.320 #> 4 21.4 6 258 110 3.08 3.215 参数可以传递给.idbind_rows,并且会生成一个新列,其中包含一个索引,每个列都有一个列表元素来自

答案 1 :(得分:5)

我可能会这样做:

l1 <- list(mtcars,mtcars)
l2 <- list(mtcars,mtcars)
do.call(rbind,mapply(FUN = cbind,l1,l2,SIMPLIFY = FALSE))

如果数据框非常大,您可以切换到cbindrbind的dplyr或data.table等效项。