rek
rek

Reputation: 187

Create a new column with a specific string in every row

In a dataframe

df <-  data.frame(id = c(12,35), col1 = c("ext", "another"))

How is it possible to add a new column which will contain in every row the same text

Example output

df <-  data.frame(id = c(12,35), col1 = c("ext", "another"), mycol = c("mytext","mytext"))

Upvotes: 0

Views: 913

Answers (2)

akrun
akrun

Reputation: 887118

We can also make use the recycling

data.frame(id = c(12,35), col1 = c("ext", "another"), mycol = "mytext")

Upvotes: 0

The following should do it.

df <-  data.frame(id = c(12,35), col1 = c("ext", "another"))

df$mycol <-  "mytext"
# id    col1  mycol
# 1 12     ext mytext
# 2 35 another mytext

Upvotes: 1

Related Questions