add a column with the same value in r

To add a column with the same value to a data frame in R, you can use the mutate() function from the dplyr package. Here's an example:

main.r
library(dplyr)

# create a sample data frame
df <- data.frame(a = 1:5, b = 6:10)

# add a new column with the same value
df <- df %>%
  mutate(c = "same_value")

print(df)
172 chars
11 lines

This will output:

main.r
  a  b          c
1 1  6 same_value
2 2  7 same_value
3 3  8 same_value
4 4  9 same_value
5 5 10 same_value
108 chars
7 lines

In this example, we first create a sample data frame called df with two columns a and b. Then, we use the mutate() function to add a new column c. The value of this column is set to "same_value". Finally, we print the resulting data frame using the print() function.

gistlibby LogSnag