data.frame issue

Hi everyone,

I am new at R programing, and I am struggling with something quite simple I assume. The idea here is to rename strings in my dataframe and then merge 2 data frames
the merging seems to work fine but the rename is causing problems

Could someone help me with that ?
thanks

setwd("~/Get consolidated file")

load xlsx library

library("xlsx")
library(stringr)

read xlsx files

read_data2 <- read.xlsx("shiny.xlsx",1, check.names = FALSE)
read_data <- read.xlsx("Tracking.xlsx",1, check.names = FALSE)

#Change and rename headers

colnames(read_data) <- gsub('StimulusConcentration', "TreatmentGroupName", colnames(read_data))
colnames(read_data2) <- gsub('StimulusConcentration', "TreatmentGroupName", colnames(read_data2))

#change all occurences from the dataframe

str_replace_all(read_data, c("TRO_0uM" = "TRO_0.03uM", "TRIN-5_0uM" = "TRIN-5_0.03uM"))

#here results are displayed with [1] "c("
and when I run read_data another dataframe without the replacements is displayed

I just need to export my dataframe with the modifications but I cannot

#Merge the 2 xlsx based on ChildSampleID
x <- merge(read_data2, read_data, by = "SampleID", all.x = TRUE, all.y = TRUE)

write.table(read_data, file = "toto.txt", quote = FALSE, sep = "\t", col.names = TRUE,
row.names = FALSE)

What happens here is that you apply a function to an object. You see the result, but if you want to save it, you must do so explicitly.

new_object <- some_function(old_object)

Here's an example

library(palmerpenguins)
data("penguins")
with(penguins, unique(species))
#> [1] Adelie    Gentoo    Chinstrap
#> Levels: Adelie Chinstrap Gentoo
with(penguins, unique(island))
#> [1] Torgersen Biscoe    Dream    
#> Levels: Biscoe Dream Torgersen
penguins$species <- gsub("Adelie","Adele",penguins$species)
penguins$island <- gsub("Biscoe","Bisquit",penguins$island)
with(penguins, unique(species))
#> [1] "Adele"     "Gentoo"    "Chinstrap"
with(penguins, unique(island))
#> [1] "Torgersen" "Bisquit"   "Dream"

Created on 2023-09-03 with reprex v2.0.2

Hi Technocrat,

Thanks so much for your help. Now it works but 2 problems occured:
Headers have disappeared
and all my table is now with the format C("

Do you know why ?

thanks a lot
V.

I can't tell without a reprex (see the FAQ) to be able to run the code. What you can do is to save the before header row with

header <- colnames(your_data)

and then

colnames(your_new_data) <- header

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.