如何通过循环将一列数据添加到现有数据帧

时间:2019-07-10 17:29:48

标签: r for-loop lapply

我需要将数据列添加到几个数据帧中,这些数据帧代表从中导出数据的染色体。我读过许多关于在数据框中添加一列的文章,它们要么太具体地回答了问题,要么切换到使用Apply函数,这些函数在它们的操作方式上非常不透明。有人可以在这里帮助我,并向我展示适当的for循环和Apply技术,以便我可以开始拆开apply + function(x)样式了吗?

我尝试使用paste0创建对象向量以插入到循环中,但是失败了。这是我认为循环应该如何工作(但失败)的基本语法:

<?php
 ob_start();
include_once __DIR__.'/header2.php';
if (!$_SESSION['u_uid']) {
     echo "<meta http-equiv='refresh' content='0;url=index.php?donation=notlogin'>";
    exit();
} else {

include_once __DIR__.'/includes/dbh.php';

 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    header("Location: index.php");
    exit();
 }

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
 curl_setopt($ch, CURLOPT_URL, 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, "cmd=_notify-validate&" . http_build_query($_POST));
 $response = curl_exec($ch);
 curl_close($ch);

 if ($response == "VERIFIED" && $_POST['receiver_email'] === "admin@pianocourse101.com") {

$cEmail = strip_tags($_POST['payer_email']);
    $firstname = strip_tags($_POST['first_name']);
    $lastname = strip_tags($_POST['last_name']);



    $price = strip_tags($_POST['mc_gross']);
    $currency = strip_tags($_POST['mc_currency']);
    $item = strip_tags($_POST['item_number']);
    $paymentStatus = strip_tags($_POST['payment_status']);




    if ($item == "Donation" && $currency == "USD" && $paymentStatus == "Completed" && $price == 100) {

        $sql = "INSERT INTO donation (user_email, firstname, lastname, amount) VALUES (?,?,?,?);";

        $stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql)) {
                       echo "SQL error";
                    } else {
                        mysqli_stmt_bind_param($stmt, "sssi", $cEmail, $firstname, $lastname, $price); 
                        mysqli_stmt_execute($stmt);


    }
}
}
}


 ?>





I don't get any errors here, will this work in a real environment? Can someone confirm if this will work with ipn or api only?

这是我通过此代码得到的错误:eval(lhs,parent,parent)中的错误:找不到对象'df.i'。

输出应如下所示:

library(dplyr)

df.1 <- data.frame(V1=rnorm(100), V2=rnorm(100))
df.2 <- data.frame(V1=rnorm(100), V2=rnorm(100))
df.3 <- data.frame(V1=rnorm(100), V2=rnorm(100))

for(i in 1:3){
  df.i <- df.i %>% mutate(CHR = i)
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可以添加列名:

column_names= ["Id", "Name", "Age", "Weight", 'mreading1', 'mreading2', 'mreading3', 'freading1', 'freading2', 'freading3']
df = pd.read_csv("defo.csv", names = column_names)

答案 1 :(得分:0)

您可以尝试将数据放入列表并按如下所示循环。

df.1 <- data.frame(V1=rnorm(100), V2=rnorm(100))
df.2 <- data.frame(V1=rnorm(100), V2=rnorm(100))
df.3 <- data.frame(V1=rnorm(100), V2=rnorm(100))
df_list <- list(df.1,df.2, df.3)
for(i in 1:length(df_list)){
  df_list[[i]] <- df_list[[i]] %>% mutate(CHR = i)
}