[英]Split named numeric vector into different elements in R
I have a named numeric vector in R with more than 2000 named numbers.我在 R 中有一个命名数字向量,其中有超过 2000 个命名数字。 I would like to split each element, in which the number is higher than 1, into the number of elements as indicated by the number, to have in the end only the number 1. But the important thing is to have also the names changed as indicated in my output.我想将数字大于 1 的每个元素拆分为数字表示的元素数量,最后只有数字 1。但重要的是名称也更改为在我的 output 中表示。
Input:输入:
obj<- c(3,1,1,1,6,1,1,1,1,1,1,1,1,3)
names(obj)<- c("V4","V10","V12","V15","V16","V20","V21","V22","V23","V36","V39","V40","V41","V42")
>obj
V4 V10 V12 V15 V16 V20 V21 V22 V23 V36 V39 V40 V41 V42
3 1 1 1 6 1 1 1 1 1 1 1 1 3
Output: Output:
obj_out<- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3)
names(obj_out)<- c("V4_1","V4_2","V4_3","V10","V12","V15","V16_1","V16_2","V16_3","V16_4","V16_5","V16_6","V20","V21","V22","V23","V36","V39","V40","V41","V42_1","V42_2","V42_3")
> obj_out
V4_1 V4_2 V4_3 V10 V12 V15 V16_1 V16_2 V16_3 V16_4 V16_5 V16_6 V20 V21
1 1 1 1 1 1 1 1 1 1 1 1 1 1
V22 V23 V36 V39 V40 V41 V42_1 V42_2 V42_3
1 1 1 1 1 1 1 1 1
I have already a code, but it names the sequence completely new, starting with V1:我已经有一个代码,但它以全新的方式命名序列,从 V1 开始:
obj_out<- paste0("V",rep(seq_along(obj_out), obj_out), "_", sequence(obj_out))
Here is one base R approach:这是一种基本的 R 方法:
#Repeat each value in obj, obj number of times
res <- rep(obj, obj)
#Change all values to 1.
res[] <- 1
#Change the names
names(res) <- ave(names(res), names(res), FUN = function(x)
if(length(x) == 1) x else paste(x, seq_along(x), sep = '_'))
res
# V4_1 V4_2 V4_3 V10 V12 V15 V16_1 V16_2 V16_3 V16_4 V16_5 V16_6
# 1 1 1 1 1 1 1 1 1 1 1 1
# V20 V21 V22 V23 V36 V39 V40 V41 V42_1 V42_2 V42_3
# 1 1 1 1 1 1 1 1 1 1 1
One option utilizing purrr
could be:利用purrr
的一种选择可能是:
imap(obj[obj > 1], ~ set_names(rep(1, .x), paste(.y, 1:.x, sep = "_"))) %>%
reduce(c) %>%
c(., obj[obj == 1])
V4_1 V4_2 V4_3 V16_1 V16_2 V16_3 V16_4 V16_5 V16_6 V42_1 V42_2 V42_3 V10 V12
1 1 1 1 1 1 1 1 1 1 1 1 1 1
V15 V20 V21 V22 V23 V36 V39 V40 V41
1 1 1 1 1 1 1 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.