简体   繁体   English

r,在data.frame中添加一列

[英]r , adding a column in data.frame

I have three data set. 我有三个数据集。

Set 1: 第一组:

   A    B
  23   12
  34   81
  13   45
  11   23

set 2: 第2组:

   A    B 
 .34    1.1
 .5     2.0
 .4     1.8

set 3: 第3组:

   A    B
  -23   5
  -3    0.1
  .8    -2.3
  -.6   1.4
  4     3.2

Now i have to create a data frame as the following type : 现在我必须创建以下类型的数据框:

   A     B    Type
   23    12   1
   34    81   1
    .
    .
    .
   11    23   1
  .34   1.1   2
   .5   2.0   2
   .4   1.8   2
  -23   5     3
    .
    .
    .
    4   3.2   3

My attempt: 我的尝试:

   set1 <- data.frame(A=c(23,34,13,11),B=c(12,81,45,23)) 
   set2 <- data.frame(A=c(.34,.5,.4),B=c(1.1,2,1.8))
   set3 <- data.frame(A=c(-23,-3,.8,-.6,4),B=c(5,.1,-2.3,1.4,3.2))

   dat123 <- rbind(set1,set2,set3)

but i couldn't able to add the column Type . 但我无法添加列Type

Like this: 像这样:

l <- list(set1, set2, set3)
do.call(rbind, Map(data.frame, l, type = seq_along(l)))
set1 <- data.frame(A=c(23,34,13,11),B=c(12,81,45,23)) 
set2 <- data.frame(A=c(.34,.5,.4),B=c(1.1,2,1.8))
set3 <- data.frame(A=c(-23,-3,.8,-.6,4),B=c(5,.1,-2.3,1.4,3.2))

Type=rep(c(1,2,3), times=c(nrow(set1), nrow(set2), nrow(set3)))
dat123 <- cbind(rbind(set1,set2,set3), Type)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM