Hi guys. I need help. I am studying R in Uni at the moment. Our professor gave CSV file as a part of the exercise. As you can see, after reading by the R studio, it appears to be a simple data frame with 2 columns. But there are errors in both columns. All values in each cell should be a 6-figure number. But some of them are 5-figure numbers. We are tasked to generate 2 correct columns based on them. We are supposed to add a "0" at the end of every 5-figure number to make them a 6-figure number. I assume that we are going to use the "if...else..." command here. But this command is the one I suck at. I can bearly use the "if...else..." statement in a stand-alone situation. There is no way I can combine it into a column-generating function. If you know how to do this, please let me know. Thanks very much.
Asked
Active
Viewed 45 times
-2
-
1There’s no need to use `if-else` here. You can instead use `sprintf` with a suitable format string which pads the number with zeros. The format string to zero-pad a number to a size of N digits is `"%0Nd"` (where `N` needs to be replaced by the desired width). – Konrad Rudolph Jul 27 '23 at 11:19
-
Hi, thanks very much for helping me. But I still don't know how to get "sprintf" to check my column. This is my first R or coding-related unit. I haven't studied any coding before. For example, my first column is called "import". What should I write for this command to check "import" column? – John W Jul 27 '23 at 11:23
-
1Can you edit your question to share a sample of your data using `dput()` instead of posting an image? It makes it much easier for us to help! – nrennie Jul 27 '23 at 11:25
-
1`sprintf()` does not “check” columns. The point of my comment was that you do not *need* to perform any checking. [Consult the documentation of `sprintf`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/sprintf.html), and of any other function you might need and which you are not familiar with. It comes with lots of examples that explains how the function works and how it is used. – Konrad Rudolph Jul 27 '23 at 11:25
-
OP are you looking to add a 0 on the left edge or the right edge of the shorter numbers? – Mark Jul 27 '23 at 11:37
-
On the right edge of the shorter number, thanks – John W Jul 27 '23 at 11:38
-
@Mark It’s frowned upon to just post ready-to-use answers for homework questions. Asking for help (and providing help) is acceptable; providing solutions is *not*. – Konrad Rudolph Jul 27 '23 at 11:46
-
1@KonradRudolph Apparently, these are numeric values. "Padding to the right" should be done by multiplication with 10 and not by coercion to characters. Testing the number of digits is trivial. The only additional knowledge needed is an understanding of `if (...) ... else ...` vs `ifelse(..., ..., ...).` – Roland Jul 27 '23 at 12:23
1 Answers
-1
There are multiple approaches to these kind of issues. For your case, easiest would be to transform the values in to strings and then do basic string manipulation.
If you are supposed to use base R functionality, the following would work: Assuming your Dataframe is calles "df"
df$import <- as.character(df$import)
df$import <- ifelse(nchar(df$import ) < 6, paste0(df$import , strrep("0", 6 - nchar(df$import ))), df$import )
df$import <- as.numeric(df$import )
Same goes for your "export" column.
As an alternative solution you can use the stringr
and dplyr
libraries.
ddf <- df %>%
mutate(across(c(col1, col2), ~str_pad(., 6, side = "right", pad = "0"))) %>%
mutate(across(c(col1, col2), as.numeric))

Michi
- 1
- 1
-
As mentioned in the comments below the question: it is *frowned upon* to provide solutions for homework questions. – Konrad Rudolph Jul 27 '23 at 13:31