r中有条件的糊剂
我的数据帧看起来像这样:
N = 1000
a <- rnorm(N)
b <- rnorm(N)
df <- data.frame(a, b)
如果a
的值小于10,我想设置有条件的粘贴
。如果a
&lt; 中的数字前面10。
我该怎么做?谢谢
My dataframe looks like this:
N = 1000
a <- rnorm(N)
b <- rnorm(N)
df <- data.frame(a, b)
I would like to set up a conditional paste0 if the value of a
is smaller than 10.
Specifically, I would like to print the value '0' (with no spaces) in front of the number in a
if a
< 10.
How can I do this? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做:
ifelse(a&lt; 0,paste0(“ 0”,a),a)
您可能需要使用
abs(a)&lt; 10
或以其他方式特别照顾负数。You could do:
ifelse(a < 0, paste0("0", a), a)
You might want to use
abs(a) < 10
or take special care of negative numbers in other ways.我使用@Harre解决方案,并以更一般的功能也实现了负面的解决方案。
有关以后的问题,请提供一个较小的数据集,您的不需要1000行数据框架。
I used @harre solution and implemented it in a more general function that take negative too.
For future questions please provide a smaller dataset, your's don't need 1000 lines dataframe.