Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate string to data frame column

Tags:

dataframe

r

I want to append to a data.frame the same string.

> df1 <- data.frame(pt1="a", pt2="b", row.names=1)
> df1
  pt1 pt2
1   a   b

As a result I would like to have:

   pt1                 pt2
1  Add this string a   Add this string b
like image 986
Carol.Kar Avatar asked Oct 31 '25 17:10

Carol.Kar


1 Answers

We can use lapply

df1[] <- lapply(df1, function(x) paste('Add this string', x))

Or use Map

df1[] <- Map(paste, 'Add this string', df1)

Or

library(dplyr)
df1 %>%
     mutate_each(funs(paste('Add this string', .)))
like image 199
akrun Avatar answered Nov 03 '25 08:11

akrun