问题
I'm trying to create a series of variables in R based on an ifelse function:
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. Right now, I just have a line for every single year.
In stata, I could do this in the following way:
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? I know for
loops aren't great. 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.
Thanks in advance for the help!
回答1:
Try:
sapply(1970:2007,function(x){ ifelse(year_begin<x & year_end>x,1,0) })
回答2:
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:
constructing the "comp+year" columns:
year<-c(1970:2007)
var<-list(length(year))
for(j in year){
var[j-1969]<-paste('comp', j)
}
filling out the "comp+year" list:
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
来源:https://stackoverflow.com/questions/35440235/creating-variables-using-loop-or-apply-in-r