[英]creating variables using loop or apply in r
I'm trying to create a series of variables in R based on an ifelse function: 我正在尝试基于ifelse函数在R中创建一系列变量:
comp1990<-ifelse(year_begin<1990 & year_end>1990,1,0)
comp1991<-ifelse(year_begin<1991 & year_end>1991,1,0)
comp1992<-ifelse(year_begin<1992 & year_end>1992,1,0)
I'm doing this for years 1970-2007. 我从事1970-2007年。 Right now, I just have a line for every single year. 现在,我每年只有一条线。
In stata, I could do this in the following way: 在stata中,我可以通过以下方式执行此操作:
forvalues n=1970(1)2007 {
gen comp\`n'== (year_begin<\`n' & year_end>\`n')
}
Is there a similarly straightforward way to do this in R ? 在R中有类似的简单方法吗? I know for
loops aren't great. 我知道for
循环不好。 Maybe using apply? 也许使用申请?
What I'm essentially doing is creating a dummy = 1 if a bank branch exists in year n and 0 otherwise (so if the bank branch was established before year n and if it closed after year n), which means it was operating in year n. 我实际上要做的是,如果第n年存在一个银行分支机构,则创建一个哑元= 1,否则创建0(因此,如果该银行分支机构是在n年之前建立的,并且如果它在n年之后关闭了),这意味着它正在某年内运营。
Thanks in advance for the help! 先谢谢您的帮助!
尝试:
sapply(1970:2007,function(x){ ifelse(year_begin<x & year_end>x,1,0) })
Here is a solution I managed to reach with the best to my understanding of the question. 这是我设法最大程度地理解该问题的解决方案。 A better description of the data would be helpful. 更好地描述数据将有所帮助。
Here is the data: 数据如下:
df<-data.frame(cbind(bank = c("bank1","bank2","bank3","bank4","bank5"),
year_begin = sample(1970:2007, 5, T),
year_end = sample(1970:2007, 5, T) ))
df$year_begin<-as.numeric(as.character(df$year_begin))
df$year_end<-as.numeric(as.character(df$year_end))
I used two for loops to build variables names as well as values: 我使用了两个for循环来构建变量名称和值:
constructing the "comp+year" columns: 构造“ comp + year”列:
year<-c(1970:2007)
var<-list(length(year))
for(j in year){
var[j-1969]<-paste('comp', j)
}
filling out the "comp+year" list: 填写“ comp + year”列表:
for(i in 1:nrow(df)){
for(j in year){
if(df$year_begin[i] < j & df$year_end[i] > j)
{var[[j-1969]]<-c(var[[j-1969]], 1)}
else
{var[[j-1969]]<-c(var[[j-1969]], 0)}
}
}
list to dataframe: 列出到数据框:
a<-do.call(rbind, var)
names<-a[,1]
values<-as.data.frame(t(a[,2:6]))
colnames(values)<-names
print(values) #you can cbind this to your original dataframe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.