Add a prefix to all rows in R
Just use paste0
to combine strings.
For example,
chrs$endsnp = paste0('end', chrs$endsnp)
or using paste
and specifing the separator between the strings
chrs$endsnp = paste('end', chrs$endsnp, sep='')
Add a prefix and suffix to each row of a dataframe but no suffix to the last row and then collapse all
I think that the best approach is to add prefix and suffix to all,
then extract the string to remove the last OR
library(tidyverse)
library(glue)
x <- data.frame(products = c("foo","bar","foobar"))
x$products <- glue("BRAND_NAME LIKE '% {x$products} %' OR ")
now collapse to single string
glue_collapse(x$products) %>%
now the string extraction
str_extract(., ".+(?= OR $)")
This last statement looks ahead for the space-OR-space at the end ($) for a match, and includes all characters up to but not including this match
Add prefix and suffix to list values R
I would vectorize this as follows
indx <- which(data[, -1] != "", arr.ind = TRUE) # Find all non-empty incidences
data[, -1][indx] <- paste("Here this is", data[, -1][indx], "completed.")
Adding a prefix only to specific rows in dataframe R
Try:
Fold_change$Well <- as.character(Fold_change$Well) # change column class from factor to character first
Fold_change$Well[1:288] <- paste0('Plate_1_', Fold_change$Well[1:288])
Add prefix to every row of a column if it doesn't contain it
Another option...
dt$value <- gsub("^([^_].*)", "_\\1", dt$value)
dt
id value
1 1 _a
2 2 _b
3 3 _c
The first regex looks for the start of a string followed by something that is not _
, plus any following characters, and replaces it with the second regex, which is _
plus the capture group from the first regex.
Adding prefix to specific R columns
You aren't obliged to use grepl()
. It's possible to use gsub()
instead. Since gsub()
if no match found, returns unchanged values, you can change your column names on the fly, without intermediate indexing step:
Example data set
sed.seed(66248086)
(
dat <- data.frame(
ID = 1:3,
EXPFRQ = runif(3),
EXPMETADAT = sample(c(T, F), 3, T),
.EXPGRP = sample(LETTERS[1:3], 3, T),
EXPOUT = sample(c('*', '**', '***'), 3, T),
NOTES = c("Bad project...", "What is in this tube?", "Blot, blot western baby")
)
)
# ID EXPFRQ EXPMETADAT .EXPGRP EXPOUT NOTES
# 1 1 0.5483680151 FALSE C * Bad project...
# 2 2 0.0628816469 FALSE B ** What is in this tube?
# 3 3 0.0001267055 TRUE B *** Blot, blot western baby
Actually where renaming takes place
Just give to gsub()
your pattern, and call the chunk it found using back-reference (\\1
):
colnames(dat) <- gsub('^(EXP[A-Z]{3})$', 'New_\\1', colnames(dat))
Result
dat
# ID New_EXPFRQ EXPMETADAT .EXPGRP New_EXPOUT NOTES
# 1 1 0.5483680151 FALSE C * Bad project...
# 2 2 0.0628816469 FALSE B ** What is in this tube?
# 3 3 0.0001267055 TRUE B *** Blot, blot western baby
Note: Please, reade the note written by @akrun under his answer!
How to merge rows with the same prefix in a data frame?
df %>%
group_by(col1 = str_remove(col1, "\\d+"))%>%
summarise(counts = sum(counts))
Adding a prefix to each column name in each dataframe in a list of dataframes R
In tidyverse
, we can use imap
with rename_with
:
library(dplyr)
library(purrr)
imap(l1, ~.x %>%
rename_with(function(x) c(paste(.y, x[1], sep = '_'), paste0('lab_', x[-1]))))
#$Fe
# Fe_Determination_No lab_2 lab_3 lab_4 lab_5 lab_7 lab_8 lab_10 lab_12
#1 1 55.94 56.83 56.39 56.32 56.48 56.382 56.3 56.11
#2 2 55.70 56.54 56.43 56.29 56.40 56.258 56.5 56.46
#3 3 56.59 56.18 56.53 56.31 56.54 56.442 56.2 56.10
#4 4 56.50 56.50 56.31 56.32 56.43 56.258 56.5 56.35
#5 5 55.98 56.51 56.47 56.39 56.73 56.532 56.7 56.36
#6 6 55.93 56.34 56.35 56.32 56.62 56.264 56.5 56.37
3$SiO2
# SiO2_Determination_No lab_2 lab_3 lab_4 lab_5 lab_7 lab_8 lab_10 lab_12
#1 1 7.63 7.84 7.67 7.91 7.77 7.936 7.872685 7.64
#2 2 7.65 7.69 7.74 7.84 7.83 7.685 7.851292 7.71
#3 3 7.73 7.59 7.62 7.96 7.76 7.863 7.872685 7.71
#4 4 7.67 7.77 7.81 7.87 7.78 7.838 7.722933 7.65
#5 5 7.67 7.74 7.66 7.84 7.65 7.828 7.680147 7.82
#6 6 7.67 7.64 7.80 7.92 7.74 7.767 7.615967 7.68
#$Al2O3
# Al2O3_Determination_No lab_2 lab_3 lab_4 lab_5 lab_7 lab_8 lab_10 lab_12
#1 1 2.01 2.01 2.00 2.02 1.88 2.053 2.002830 2.09
#2 2 2.02 2.01 2.03 2.02 1.90 2.044 2.021725 2.05
#3 3 2.03 2.00 1.99 2.05 1.89 2.041 2.021725 1.96
#4 4 2.01 2.02 2.01 2.03 1.88 2.038 1.983936 2.09
#5 5 2.02 2.02 2.01 2.02 1.88 2.008 2.002830 2.06
#6 6 2.00 2.03 2.01 2.03 1.87 2.020 2.021725 2.02
Or in base R with Map
:
Map(function(x, y) {
names(x) <- c(paste(y, names(x)[1], sep = '_'), paste0('lab_', names(x[-1])))
x
}, l1, names(l1))
Add a prefix to column names
You have misread the help file. Here's the argument to look at:
do.NULL
: logical. If FALSE
and names are NULL
, names are created.
Notice the and in that description. Your names are no longer NULL
, so using prefix won't work.
Instead, use something like this:
> m2 <- cbind(1,1:4)
> colnames(m2) <- c("x","Y")
> colnames(m2) <- paste("Sub", colnames(m2), sep = "_")
> m2
Sub_x Sub_Y
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
Related Topics
Compute Monthly Averages from Daily Data
R: Ggplot Stacked Bar Chart with Counts on Y Axis But Percentage as Label
Adding Column If It Does Not Exist
How to Rbind Vectors Matching Their Column Names
Pad with Leading Zeros to Common Width
How to Strip Dollar Signs ($) from Data/ Escape Special Characters in R
Dealing with Very Small Numbers in R
How to Conditionally Highlight Points in Ggplot2 Facet Plots - Mapping Color to Column
How to Use Empty Space Produced by Facet_Wrap
Output in R, Avoid Writing "[1]"
Find Out the Number of Days of a Month in R
How to Use Dplyr's Summarize and Which() to Lookup Min/Max Values
Merge Dataframes, Different Lengths
Combining S4 and S3 Methods in a Single Function
Edit Datatable in Shiny with Dropdown Selection for Factor Variables