rep()函数使用变量作为'times'参数时出现错误

3
我有一个数据集 (假设) test:
test <- data.frame(x = c(90, 801, 6457, 92727), y = rep("test", 4))
print(test)
      x    y
1    90 test
2   801 test
3  6457 test
4 92727 test

我想创建一个名为test$z的变量,它与test$x相同,不同之处在于test$z始终是10个字符长,并用零填充空缺。因此,生成的数据框将如下所示:

print(test)
      x    y          z
1    90 test 0000000090
2   801 test 0000000801
3  6457 test 0000006457
4 92727 test 0000092727

我认为以下函数会给我想要的结果:

test$z <- paste0(as.character(rep(0, 10-nchar(as.character(test$x)))), as.character(test$x))

但是在rep函数中出现以下错误:

在 rep(0, 10 - nchar(as.character(test$x))) 中出错:
times 参数无效

有什么不同的方法可以使用rep函数或其他解决方案来获得test$z?


2
你可以使用 sprintf。 - Roland
formatC(test$x, flag = '0', digits = 10, width = 10) - rawr
3个回答

4
问题出在rep(0, 10-nchar(as.character(test$x))),其中第二个参数是一个向量,即times参数。基本上,这会抛出错误:
rep(0, c(9, 8, 7, 4))

相反,你应该这样做:

rep(c(0,0,0,0), c(9, 8, 7, 4))

其中两个向量的长度相同。

?rep表示:

如果times只包含一个整数,则结果是将整个输入重复此次数。如果times是与x(经过每个副本的复制)具有相同长度的向量,则结果由x [1]重复times [1]次,x [2]重复times [2]次等组成。

在我们的示例中,xc(0,0,0,0),而timesc(9,8,7,4)

你可以这样做:

test$z <- sapply(test$x, function(x) paste0(paste0(rep(0,10-nchar(x)),collapse = ""),x))

#      x    y          z
#1    90 test 0000000090
#2   801 test 0000000801
#3  6457 test 0000006457
#4 92727 test 0000092727

3

在评论中,@Roland提到了 sprintf(),这是一个很好的想法。而@m0h3n在他的回答中解释了rep()的问题。下面是两者的替代方案。

你可以使用新的基础函数strrep()来取代rep(),它将会重复x参数times次数。对于你的情况,它似乎可以完美地工作。

strrep(0, 10 - nchar(test$x))
# [1] "00000000" "0000000"  "000000"   "00000"   

因此,我们只需将其粘贴到 test$x 的前面,就完成了操作。不需要任何 as.character 强制转换,因为这一切都是在内部完成的。
paste0(strrep(0, 10 - nchar(test$x)), test$x)
# [1] "0000000090" "0000000801" "0000006457" "0000092727"

注意:`strrep()`在R版本3.3.1中引入。

2

到目前为止,你已经得到了几个不错的答案。

有趣的是,这里有一个例子,展示了一种使用你可能已经掌握的函数进行“快速脏短”的方法。

test$z <- substr(paste0('0000000000', as.character(test$x)),
                 nchar(test$x),
                 10+nchar(test$x))

只需在每个条目中粘贴比所需的零 (例如10) 更多的零并进行子字符串处理。

附注:您可以通过使用长度为 n 的字符串替换上述代码中的零字符串来代替上述操作:

paste0(rep(0, n), collapse='')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接