简体   繁体   English

创建一个新变量,该变量从现有数据框中获取不同的列,具体取决于分组变量

[英]Creating a new variable which takes different columns from an existing on data frame, dependent on a grouping variable

I had no idea where to start with this code. 我不知道从哪里开始使用这段代码。 I want to attach a new variable to an existing data frame which takes different columns depending on a grouping variable. 我想将一个新变量附加到现有数据框架,该数据框架根据分组变量采用不同的列。 For example, say I have columns 例如,假设我有列

    A  B  C  D  E  F
    1  2  3  6  11 12
    1  7  5  10 8  9
    2  19 2  4  5  6
    2  8  4  3  1  1

I want to attach a new column "G" which is column B if A is 1 and column D if A is 2 我想附加一个新列“G”,如果A是1则是B列,如果A是2,则是D列

    A  B  C  D  E  F   G
    1  2  3  6  11 12  2 
    1  7  5  10 8  9   7
    2  19 2  4  5  6   4
    2  8  4  3  1  1   3

thanks 谢谢

Here are a couple of options. 这里有几个选项。

assuming your data.frame is called DF 假设您的data.frame被称为DF

Basic [ and indexing 基本[和索引

# make everything in G =  B
DF$G <- DF$B
# replace those cases where A==2 with D
DF$G[DF$A==2] <- DF$D[DT$A==2]

using ifelse 使用ifelse

Only one ifelse statement is required as A is either 1 or 2 只需要一个ifelse语句,因为A是1或2

DF$G <- ifelse(DF$A==2, DF$D, DF$B)

using a data.table 使用data.table

I like data.table, for memory efficiency and coding elegance 我喜欢data.table,用于内存效率和编码优雅

library(data.table)
# create a data.table with A as the key

DT <- data.table(DF, key = 'A')
# where the key (A) == 1 ], then assign G = B
DT[.(1), G := B]
# and where the key (A) == 2, then assign G = D
DT[.(2), G := D]

beautifully elegant! 精美优雅!

Assuming your data.frame is called "mydf", you can use ifelse : 假设您的data.frame被称为“mydf”,您可以使用ifelse

within(mydf, {
  G <- ifelse(A == 1, B,
              ifelse(A == 2, D, 
                     0))
})
#   A  B C  D  E  F G
# 1 1  2 3  6 11 12 2
# 2 1  7 5 10  8  9 7
# 3 2 19 2  4  5  6 4
# 4 2  8 4  3  1  1 3

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

相关问题 从 R 的数据框中的现有列创建新的竞争变量(使用 case_when 函数) - Creating a New Race Variable from Existing Column in Data Frame in R (with case_when function) 通过使用for循环对数据帧的列求和来创建新变量 - Creating new variable by summing the columns of the data frame using for loop 从现有行和列中的值创建新变量 - Creating a new variable from values in existing rows and columns 从变量值创建一个新的 Data.Frame - Creating a new Data.Frame from variable values 使用另一个变量中的公式为数据框创建新列 - Creating a new column to a data frame using a formula from another variable 将数据框中的列/变量追加到新变量中 - Appending columns/variables from a data frame into a new variable 在函数中创建新的,与变量名称相关的列(以指示表达式数据中的重要性级别) - Creating new, variable-name dependent columns in a function (to indicate levels of significance in expression data) 根据df中不同列中的其他变量创建新变量 - creating new variable based on other variables from different columns in df 使用分组变量按列拆分data.frame - split a data.frame by columns using a grouping variable 从现有数据帧中以一定质量在数据帧中创建新变量/列 - Make a new variable/column in a data frame with a certain quality from an existing data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM