填充data.frame列中的字符串

时间:2016-01-14 16:16:33

标签: r

我有一个data.frame X,其中A列填充了chr,其中大多数都是nchar = 5,但有些是nchar = 4。我想在0面前放一个foreach( element_of_X$A as a ){ # this line is pseudo-code for Idk how to do it in R if(nchar(a) < 5){ # I think these lines are valid paste0(0,a) # I think these lines are valid } }

我会使用以下类型的伪代码:

[INFO   ] [Logger      ] Record log in /home/etienne/.kivy/logs/kivy_16-01-14_51.txt
[INFO   ] [Kivy        ] v1.9.1
[INFO   ] [Python      ] v2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
[INFO   ] [Factory     ] 179 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_gif, img_pil (img_pygame, img_ffpyplayer ignored)
[CRITICAL] [Window      ] Unable to find any valuable Window provider at all!
egl_rpi - ImportError: cannot import name bcm
  File "/home/etienne/Demo/Test/test_virtualenv/lib/python2.7/site-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "/home/etienne/Demo/Test/test_virtualenv/lib/python2.7/site-packages/kivy/core/window/window_egl_rpi.py", line 12, in <module>
    from kivy.lib.vidcore_lite import bcm, egl

pygame - ImportError: No module named pygame
  File "/home/etienne/Demo/Test/test_virtualenv/lib/python2.7/site-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "/home/etienne/Demo/Test/test_virtualenv/lib/python2.7/site-packages/kivy/core/window/window_pygame.py", line 8, in <module>
    import pygame

x11 - ImportError: No module named window_x11
  File "/home/etienne/Demo/Test/test_virtualenv/lib/python2.7/site-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)

[INFO   ] [Text        ] Provider: pil(['text_pygame'] ignored)
[CRITICAL] [App         ] Unable to get a Window, abort.
 Exception SystemExit: 1 in 'kivy.properties.dpi2px' ignored
[CRITICAL] [App         ] Unable to get a Window, abort.
 Exception SystemExit: 1 in 'kivy.properties.dpi2px' ignored
[CRITICAL] [App         ] Unable to get a Window, abort.
 Exception SystemExit: 1 in 'kivy.properties.dpi2px' ignored
[CRITICAL] [App         ] Unable to get a Window, abort.

(显然我来自PHP)。我怎么能用干净的R代码呢? (那,或者更有效的解决方案)

由于

5 个答案:

答案 0 :(得分:5)

实际上sprintf对我没用,所以如果你不介意共同依赖:

#reproducible example -- this happens with zip codes sometimes
X <- data.frame(A = c('10002','8540','BIRD'), stringsAsFactors=FALSE)

# X$A <- sprintf('%05s',X$A) didn't work for me
# Note in ?sprintf: 0: For numbers, pad to the field width with leading zeros. 
# For characters, this zero-pads on some platforms and is ignored on others.

library('stringr')
X$A <- str_pad(X$A, width=5, side='left', pad='0')
X
#      A
#1 10002
#2 08540
#3 0BIRD

或者,如果您更喜欢基础解决方案,则以下内容是等效的:

X$A <- ifelse(nchar(X$A) < 5, paste(c(rep("0",5-nchar(X$A)), X$A), collapse=""), X$A)

(注意这适用于长度为4或更短的字符串,而不仅仅是4)

答案 1 :(得分:2)

这应该可以解决问题:

X$A <- ifelse(nchar(X$A) < 5, paste("0", X$A, sep=""), X$A)

答案 2 :(得分:2)

如果您使用dplyrstringr,则可以执行以下操作

library(dplyr)
library(stringr)

## Assuming "element_of_X" has element 'A'
element_of_X <-
    element_of_X %>%
    mutate(A = str_pad(A, 5, side = 'left', pad = '0'))

答案 3 :(得分:1)

尝试这样的事情(假设数据框名称和列名是正确的):

element_of_X$a <- with(element_of_X , ifelse(nchar(a) == 4, paste('0', a, sep = ''), a)

答案 4 :(得分:0)

library(stringr)
x$A=str_pad(x$A, 5, pad = "0")