简体   繁体   English

对R中列中的组执行功能

[英]Perform function over groups in columns in R

I am completely new to R and have a question about performing a function over a column. 我对R完全陌生,并且对在列上执行函数有疑问。

data <- read.table(text ="group;  val
                a;  4
                a;  24
                a;  12
                b;  1
                a;  2
                c;  4
                c;  5
                b;  6 ", sep=";", header=T,stringsAsFactors = FALSE)

How could I add data in the following way? 如何以以下方式添加数据?

I would like to create two new columns which I am doing like this: 我想创建两个新的列,如下所示:

data$col1 <- 0
data$col2 <- 1

What I now want to do is to add +2 for each group value into the new columns and reach the following pattern: 我现在想做的是为每个组值添加+2到新列中,并达到以下模式:

group val   col1 col2
  a     4     0   1
  a    24     0   1
  a    12     0   1
  b     1     2   3
  a     2     0   1
  c     4     4   5
  c     5     4   5
  b     6     2   3

How could I do this? 我该怎么办? I hope I made my example more or less clear. 我希望我的例子或多或少清楚。

Try this: 尝试这个:

Creating an index to cumulatively add +2 depending on the number of groups 创建索引以根据组的数量累加+2

indx <- c(0, 2 * seq_len(length(unique(data[, 1])) - 1)) 

Splitting the data set by groups, adding (cumulatively) +2 and unsplitting back so everything comes back in place 按组拆分数据集,(相加) +2然后不拆分,以便一切恢复原状

data[, 3:4] <- unsplit(Map(`+`, split(data[, 3:4], data[, 1]), indx), data[, 1]) 
data
#   group val col1 col2
# 1     a   4    0    1
# 2     a  24    0    1
# 3     a  12    0    1
# 4     b   1    2    3
# 5     a   2    0    1
# 6     c   4    4    5
# 7     c   5    4    5
# 8     b   6    2    3

Or you could do 或者你可以做

within(data, {col1 <- 2*(as.numeric(factor(group))-1)
                  col2 <- col1+1})[,c(1:2,4:3)]
#  group val col1 col2
#1     a   4    0    1
#2     a  24    0    1
#3     a  12    0    1
#4     b   1    2    3
#5     a   2    0    1
#6     c   4    4    5
#7     c   5    4    5
#8     b   6    2    3

Using data.table 使用data.table

library(data.table)
 setDT(data)[,c('col1', 'col2'):= {list(indx=2*(match(group,
                                    unique(group))-1), indx+1)}]

data
#   group val col1 col2
#1:     a   4    0    1
#2:     a  24    0    1
#3:     a  12    0    1
#4:     b   1    2    3
#5:     a   2    0    1
#6:     c   4    4    5
#7:     c   5    4    5
#8:     b   6    2    3

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

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