R,在data.frame中复制rownames

时间:2016-05-07 10:07:44

标签: r dataframe

我的 data.frame 维度 [6587 37], rownames 必须在每18行后重复一次。我如何在Rstudio中做到这一点。

2 个答案:

答案 0 :(得分:1)

data.frames中的行名必须是唯一的。

$all_data = ["admin","member","editor"];
$selected = ["admin","member"];

您可以使用> df <- data.frame(x = 1:2) > rownames(df) <- c("a", "a") Error in `row.names<-.data.frame`(`*tmp*`, value = value) : duplicate 'row.names' are not allowed In addition: Warning message: non-unique value when setting 'row.names': ‘a’ 使名称唯一,但仍然带有一些重复信息。

make.names

可以在> make.names(c("a","a"), unique = TRUE) [1] "a" "a.1" 的帮助下识别这些内容 或者你可以在df中创建一个列,或者在第二个data.frame中创建一个包含信息的列

答案 1 :(得分:1)

如果你的18个列名是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class StartExam : System.Web.UI.Page
{
int totalsec = 0, sec = 0, min = 0;
String time = "";
static int j = 0;
static int i,total;
SqlConnection con;
static SqlDataAdapter da,da1;
static DataSet ds,ds1;
SqlCommand cmd;
String date;
String str,str1;

protected void Page_Load(object sender, EventArgs e)
{

    date = DateTime.Now.ToShortDateString();
    con = new SqlConnection("Data Source=.;Initial Catalog=OnlineExam;Integrated Security=True");
    con.Open();

    if (!Page.IsPostBack)
    {
        Session["time"] = 900;
        i = 0;
        total = 0;

        string str = "Select top 15 * from Question_Master where Que_Topic='" + Session["Topic"].ToString() + "' order by newid()";

        SqlDataAdapter da = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();

        da.Fill(ds, "Question_Master");

        if (ds.Tables[0].Rows.Count > 0)
        {
            DataRow dr;
            while (i < ds.Tables[0].Rows.Count)
            {
                dr = ds.Tables[0].Rows[i];
                // Session["Answer"] = Convert.ToString(Convert.ToInt32(dr["Que_Answer"].ToString()) - 1);

                QueLabel.Text = dr["Que_Question"].ToString();
                OptionRadioButtonList.ClearSelection();
                OptionRadioButtonList.Items.Clear();
                       OptionRadioButtonList.Items.Add(dr["Que_Option1"].ToString());
                OptionRadioButtonList.Items.Add(dr["Que_Option2"].ToString());
                OptionRadioButtonList.Items.Add(dr["Que_Option3"].ToString());
                  OptionRadioButtonList.Items.Add(dr["Que_Option4"].ToString());
                i++;



                //cmd = new SqlCommand("Insert into Temp_Result(TR_Question,TR_Option1,TR_Option2,TR_Option3,TR_Option4,TR_Answer,TR_Id) values('" + ds.Tables[0].Rows[j][2].ToString().Trim() + "','" + ds.Tables[0].Rows[j][3].ToString().Trim() + "','" + ds.Tables[0].Rows[j][4].ToString().Trim() + "','" + ds.Tables[0].Rows[j][5].ToString().Trim() + "','" + ds.Tables[0].Rows[j][6].ToString().Trim() + "','" + ds.Tables[0].Rows[j][7].ToString().Trim() + "'," + Convert.ToInt32(Session["Id"].ToString()) + ")", con);
                //cmd.ExecuteNonQuery();





                // String str1 = "Select top 15 * from Temp_Result where TR_Id=" + Session["Id"].ToString();
                // SqlDataAdapter da1 = new SqlDataAdapter(str, con);
                // DataSet ds1 = new DataSet();

                // da1.Fill(ds1, "Temp_Result");

                // if (ds1.Tables[0].Rows.Count > 0)
                // {
                // DataRow dr1;
                //  while (i < ds.Tables[0].Rows.Count)
                //  {
                //dr1 = ds1.Tables[0].Rows[i];


                  con.Close();

                // }

                // }
            }
        }
    }
}






protected void EndExamButton_Click(object sender, EventArgs e)
{
    Response.Redirect("~\\Result.aspx");
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    Session["time"] = Convert.ToInt16(Session["time"]) - 1;

    if (Convert.ToInt16(Session["time"].ToString()) <= 0)
    {
        Response.Redirect("~\\Result.aspx");
    }
    else
    {
        totalsec = Convert.ToInt16(Session["time"]);
        sec = totalsec % 60;
        min = totalsec / 60;
        time = min + ":" + sec;
        TimerLabel.Text = time;
    }
}
protected void SESubmitButton_Click(object sender, EventArgs e)
{
    try
   {
        if (OptionRadioButtonList.SelectedItem.Text == ds1.Tables[0].Rows[j][6].ToString())
        {
            total++;

        }
        else
        {

            total += 0;

        }
   cmd = new SqlCommand("update Temp_Result set TR_AttemptedAns='" + OptionRadioButtonList.SelectedItem.Text + "' where TR_Question='" + QueLabel.Text + "' and TR_Id=" + Convert.ToInt32(Session["Id"].ToString()) + "", con);
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    { }
    finally
    {
        if (i < ds1.Tables[0].Rows.Count - 1)
        {
            i++;
            OptionRadioButtonList.SelectedIndex = -1;
        }
        else
        {
            Response.Redirect("~\\Result.aspx?total=" + total);
        }
    }

  }

protected void SENextButton_Click(object sender, EventArgs e)
{
    //int i=0;
    //if (i < ds.Tables[0].Rows.Count - 1)
  //  {
        Response.Redirect(Request.RawUrl);
      //  i++;
       /* QueLabel.Text = ds.Tables[0].Rows[i]["Que_Question"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option1"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option2"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option3"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option4"].ToString(); */         

  //  }


}

protected void SEPreviousButton_Click(object sender, EventArgs e)
{

    if (i == ds.Tables[0].Rows.Count - 1 || i != 0)
    {
        i--;

        /*QueLabel.Text = ds.Tables[0].Rows[i]["Que_Question"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option1"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option2"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option3"].ToString();
        OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option4"].ToString();*/
    }

}

protected void OptionRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
{

    if (ds1.Tables[0].Rows.Count > 0)
   {
        OptionRadioButtonList.Items[2].Enabled = false;
        OptionRadioButtonList.Items[3].Enabled = false;
    }
    else
    {
        OptionRadioButtonList.Items[2].Enabled = true;

        OptionRadioButtonList.Items[3].Enabled = true;
    }


      if (OptionRadioButtonList.SelectedItem.Text == "")
            SESubmitButton.Enabled = true;
       else
            SESubmitButton.Enabled = false;

    }

protected void EndExamButton_Click1(object sender, EventArgs e)
{
    Response.Redirect("~\\Result.aspx?total=" + total);
}


}

你可以得到你想要的东西:

mynames <- c("a", "b", "c", "d", "e", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s")

或者您可以修改粘贴不同内容的名称。

相关问题