In .NET, I can use string.PadLeft()
and string.PadRight()
to pad a string with spaces on the left/right.
var myString = "test";
Console.WriteLine(myString.PadLeft(10)); //prints " test"
Console.WriteLine(myString.PadLeft(2)); //prints "test"
Console.WriteLine(myString.PadLeft(10, '.')); //prints "......test"
Console.WriteLine(myString.PadRight(10, '.')); //prints "test......"
What is the equivalent in R?
Use sprintf
, which is built into R:
# Equivalent to .PadLeft.
sprintf("%7s", "hello")
[1] " hello"
# Equivalent to .PadRight.
sprintf("%-7s", "hello")
[1] "hello "
Note that, like .NET, the number specified is the total width that we want to fit our text into.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With