r How to change vector values? -
how change 3 values set in vector, eg:
v=c(1,1,1,1,1,6,2,3,4,4,4,4,4,2) r=v[which(v %in% c(6,2,3))] = c(6,0,8) r [1] 1 1 1 1 1 6 0 8 4 4 4 4 4 6
the result comes warning: number of items replace not multiple of replacement length.
the idea result need :
[1] 1 1 1 1 1 6 0 8 4 4 4 4 4 2
i want 3 values change when set/group, not individually, suggestion great appreciated!
edit: i'm sorry guys, there more 1 set of 6,2,3 in data, example should like:
v=c(1,1,2,1,1,6,2,3,4,4,4,4,4,2,6,2,3,4)
and result be:
[1] 1 1 2 1 1 6 0 8 4 4 4 4 4 2 6 0 8 4
a quite different way using regular expressions:
as.numeric(strsplit(gsub("6 2 3", "6 2 8", paste(v, collapse = " ")), " ")[[1]]) # [1] 1 1 1 1 1 6 2 8 4 4 4 4 4 2
this replace instances of c(6, 2, 3)
.
Comments
Post a Comment