Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append values to vector by for loop not working

Basically, I want to take elements in vector k, but not in vector l, and append them to vector h. Here is my code using for loop:

k=c(1,2,3,5,8,9)
l=c(3,5,7,5,7,9,64)
h=c()

for (i in k) {
  if (!(i %in% l)) {
    print(i)
    append(h,i)
  }
}

After run the code, vector h does not change at all, but it should be c(1,2,8).

like image 440
minhsphuc12 Avatar asked Sep 21 '25 11:09

minhsphuc12


1 Answers

With append you need to assign the result

k=c(1,2,3,5,8,9)
l=c(3,5,7,5,7,9,64)
h=c()

for (i in k) {
  if (!(i %in% l)) {
    print(i)
    h<-append(h,i)
  }
}
like image 131
keegan Avatar answered Sep 23 '25 06:09

keegan