I want R to give the numerical data of CSV data a previous zero.

Asked 2 years ago, Updated 2 years ago, 351 views

I want to give the numerical data of CSV data a previous zero.I'd like to add the previous 0 to the single digit number.
Please tell me who it is.

r

2022-09-30 21:54

1 Answers


Included in tidyverse to change the "appearance" of the number The library(stringr) str_pad() is useful.

 pacman:p_load(tidyverse)

>somedata<-10
>somedata
[1] 10

>str(somedata)# data is numeric
 num10
>str_pad(somedata,6,pad="0")
[1] "000010"

>str_pad(somedata,6,pad="0")%>%str 
 chr "000010"
# The data has been converted to a character

The numeric object can only hold pure numbers, so

to the character type to add unnecessary zeros Please note that the results have been converted.

The simplest way to distinguish is to see if the output of the results is enclosed in double quotes.

[1]10
[1] "000010"


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.