[英]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
[
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]
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)
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.