Home > front end >  adding a specific value in column data frame containing number and character
adding a specific value in column data frame containing number and character

Time:02-08

I'm facing some data manipulation difficulties.

This is part of my data frame:


df <- structure(list(Residue = c("R-A-40", "R-A-350", "R-B-468", "R-C-490", 
"R-A-610"), Energy = c(-3.45, -1.89, -0.25, -2.67, -1.98), Model = c("DELTA", 
"DELTA", "DELTA", "DELTA", "DELTA")), class = "data.frame", row.names = c(NA, 
-5L))

> df
  Residue Energy Model
1  R-A-40  -3.45 DELTA
2 R-A-350  -1.89 DELTA
3 R-B-468  -0.25 DELTA
4 R-C-490  -2.67 DELTA
5 R-A-610  -1.98 DELTA

I would like to add (sum) 4 in all numbers in column "Residue". The output would be like this:

> df_new
  Residue Energy Model
1  R-A-44  -3.45 DELTA
2 R-A-354  -1.89 DELTA
3 R-B-472  -0.25 DELTA
4 R-C-494  -2.67 DELTA
5 R-A-614  -1.98 DELTA

I have no idea how to do this since it mixes number and characters.

I appreciate any help

Thanks in advance

CodePudding user response:

Here's a way using tidyverse tools. This presumes that the Residue variable always has the same format as what you provided.

library(dplyr)
library(stringr)

df |> 
  mutate(Residue_val = str_extract(Residue, "\\d.*$"), 
         Residue_val = as.numeric(Residue_val)   4, 
         Residue = paste0(str_remove_all(Residue, "\\d"), Residue_val)) |> 
  select(-Residue_val)

  Residue Energy Model
1  R-A-44  -3.45 DELTA
2 R-A-354  -1.89 DELTA
3 R-B-472  -0.25 DELTA
4 R-C-494  -2.67 DELTA
5 R-A-614  -1.98 DELTA
  •  Tags:  
  • Related