have fun have fun - 1 year ago 132
R Question

Improve my coding "for loop"

The following is a simple loop to insert a new column in a data frame after checking a specific condition (if 2 consecutive rows have the same value).
The code works just fine but I would like to improve my coding skills so I ask for alternative solutions (faster, more elegant).
I checked previous threads on the topic and learned a lot but I am curious about my specific case.
Thanks for any input.

vector<-1
vector_tot<-NULL

for(i in 1:length(dat$Label1))
{
vector_tot<-c(vector_tot,vector)
if(dat$Label1[i]==dat$Label1[i+1]){
vector<-0
}
else {
vector<-1
}
}


dat$vector<- vector_tot

Answer Source

For many things in R, you do not need a for loop, since functions are vectorized. So we can achieve what you want with:

# sample data
dat = data.frame(Label1=c("A","B","B","C","C","C","D"),stringsAsFactors = F)

# first create a vector that contains the previous value
dat$next_element = c(dat$Label1[2:nrow(dat)],"")

# then check if they match
dat$vector = as.numeric(dat$Label1==dat$next_element)

Output:

  Label1 next_element vector
1      A            B      0
2      B            B      1
3      B            C      0
4      C            C      1
5      C            C      1
6      C            D      0
7      D                   0

It can also be done in one line, but I think the above illustrates better how it works:

dat$vector = dat$Label1==c(dat$Label1[2:nrow(dat)],"")

Or compare with the previous element:

dat$vector = dat$Label1==c("",dat$Label1[1:nrow(dat)-1])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download