How can I create a new column containing a constant value like 100?
Example: Number 100 100 100 100
With dplyr
, you can use mutate
and assign a constant number to a new variable. If you only provide one number (like 100 below), then each row will be filled in with that given number.
library(dplyr)
df %>%
mutate(newCol = 100)
Output
x newCol
1 1 100
2 2 100
3 3 100
4 4 100
5 5 100
6 6 100
7 7 100
8 8 100
9 9 100
10 10 100
Or in base R, you can create a copy of the dataframe, then create the new column:
df2 <- df
df2$Number <- 100
Data
df <- data.frame(x = c(1:10))
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