我想把向量的一半赋值为1,剩下的赋值为o。
df <- c("1","a","b","2")
[1] "1" "a" "b" "2"
预期结果
[1] "1" "1" "0" "0"
非常感谢你提前
发布于 2020-04-07 21:16:12
使用rep
rep(1:0, each = length(df)/2, length.out = length(df))
#[1] 1 1 0 0
如果1和0的赋值方式无关紧要,那么可以只指定length.out
rep(1:0, length.out = length(df))
#[1] 1 0 1 0
发布于 2020-04-07 21:42:55
另外,稍微不同的方式是:
'<-'(df, +(seq_along(df) <= length(df) %/% 2))
#df
#[1] 1 1 0 0
https://stackoverflow.com/questions/61080844
复制相似问题