have fun have fun - 2 years ago 167
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.


for(i in 1:length(dat$Label1))
else {

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)


  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