简体   繁体   English

在[R]中,为组的每个值生成新变量

[英]In [R], gen new variable for each value of group

I have id variable and date variable where there are multiple dates for a given id (a panel). 我有id变量和date变量,其中给定id(一个面板)有多个日期。 I would like to generate a new variable based on whether ANY of the years for a given id meet a logical condition. 我想根据给定ID的年份中的任何年份是否满足逻辑条件来生成一个新变量。 I am not sure of how to code it so please don't take the following as R code, just as logical pseudocode. 我不确定如何编码,因此请不要将以下内容作为逻辑代码作为R代码。 Something like 就像是

foreach(i in min(id):max(id)) {
if(var1[yearvar[1:max(yearvar)]=="A") then { newvar==1}
}

As an example: 举个例子:

ID     Year     Letter
1     1999        A
1     2000        B
2     2000        C
3     1999        A

Should return newvar 1 1 0 1 应该返回newvar 1 1 0 1

Since data[ID==1] contains A in some year, it should also ==1 in 2000 despite Letter==B that year. 由于data[ID==1]在某年中包含A,因此即使Letter==B在那一年,它在2000年也应==1

Here's a solution using plyr : 这是使用plyr的解决方案:

library(plyr)
a <- ddply(dat, .(ID), summarise, newvar = as.numeric(any(Letter == "A")))
merge(ID, a, by="ID")

Here's a way of approaching it with base R: 这是使用基数R进行处理的一种方法:

#Find which ID meet first criteria
withA <- unique(dat$ID[dat$Letter == "A"])

#add new column based on whether ID is in withA
dat$newvar <- as.numeric(dat$ID %in% withA)

#    ID Year Letter newvar
# 1  1 1999      A      1
# 2  1 2000      B      1
# 3  2 2000      C      0
# 4  3 1999      A      1

Without using a package: 不使用包:

dat <- data.frame(
    ID = c(1,1,2,3),
    Year = c(1999,2000,2000,1999),
    Letter = c("A","B","C","A")
)
tableData <- table(dat[,c("ID","Letter")])
newvar <- ifelse(tableData[dat$ID,"A"]==1,1,0)
dat <- cbind(dat,newvar)

#  ID Year Letter newvar
#1  1 1999      A      1
#2  1 2000      B      1
#3  2 2000      C      0
#4  3 1999      A      1

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

相关问题 基于R中每个组中另一个变量的首次出现的新变量 - New variable based on first appearance of another variable in each group in R gen新变量有条件等于r中的旧变量 - gen new variable conditionally equal old variable in r 使用mutate使用R中每个组的第一个值创建一个新列 - Using mutate to create a new column with the first value of each group in R 为R中的每个组在新列中存储给定列的第三个值 - Storing third value of given column in new column for each group in R 是否有用于减去一组变量的每个值的不同中位数的 R 函数? - Is there an R function for subtracting different medians for each value of a group of a variable? 在R中的变量上选择具有特定值的每个组的最后两行 - Select last two rows of each group with certain value on a variable in R R:按组,测试是否对于一个变量的每个值,该值存在于另一个变量中 - R: By group, test if for each value of one variable, that value exists in another variable R 工作室:分解 / pivot 和 dataframe 以计算每个变量在每个组的不同时间段的平均值 - R studio: Decompose / pivot a dataframe to calculate the mean value for each variable at various time segments for each group 为 R 中的每个组添加和复制一个新列 - Add and replicate a new column for each group in R r中每个变量的表汇总数据 - Summarizing data in table by group for each variable in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM