Replacing a string of text in a line of code with another string of text.

Hi,

Is there a somewhat simple function that can take a line of code, such as...

ICP_M = summary(Data$ICP[Data$Alive_Dead.M=="Alive"])

And replace all the “ICP” with another string of text, such as “MAP,” and rerun the line of code. So, automatically change this line of code to…

MAP_M = summary(Data$MAP[Data$Alive_Dead.M=="Alive"])

And run it.

Any ideas would be greatly appreciated!

?stringi::stri_replace_all()

Regards,
Grzegorz

Convert your code into a function and then run it iteratively over a list of parameters, for example:

library(purrr)

var_summary <- function(Data, var) {
    assign(paste0(var, "_M"),
           summary(Data[[var]][Data$Alive_Dead.MV == "Alive"]),
           envir = globalenv())
}

parameters <- c("ICP", "MAP")

walk(parameters, ~var_summary(Data, .x))
1 Like

Hi andresrcs,

Thank you, I was able to use what you wrote for a lot of my functions.
I did run into some issues for some lines though. I have created a simplified version of what I am trying to do...

When I run this I receive the following error...

Error in rbind(Table, c("var", var_A_1M, var_D_1M)) :
object 'var_A_1M' not found

Not sure if I am doing this completely wrong :/. I'm sort of a noob at all of this haha.

Thanks in advance :slight_smile:

Sorry but I can't find the logic in your code, can you clarify what the output of the function should be or how it should looks like?. Preferably as a proper REPRoducible EXample (reprex) illustrating your issue.

Hi andresrcs,

Sorry for the confusion, I'm not the best at explaining what I am trying to do :sweat_smile:

Basically I have a bunch of variables...
height_A_1M, height_D_1M, height _AD_1M, height_F_1M, height_U_1M, height _FU_1M,
weight_A_1M, weight_D_1M, weight _AD_1M, weight_F_1M,weight_U_1M, weight _FU_1M,
age_A_1M, age_D_1M, age _AD_1M, age_F_1M, age_U_1M, age _FU_1M
(and so on with a bunch of other variables)

I need to create a row matrix for each variable.

For example: for height I would want...
Row_height = c(height_A_1M, height_D_1M, height _AD_1M, height_F_1M, height_U_1M, height _FU_1M)

But since I will be having a bunch of variables, I was hoping to find a function that can do this automatically for all the variables haha.

This is what I am trying at the moment, but it isn't working :frowning: ...

Thanks for all the help so far!!!

R is not exactly meant to be used this way, R is mostly vectorized so manually defining data structures one element at a time doesn't seem like the best way of doing what you are doing (although, I don't really understand what you are doing).

If you can give a higher level explanation of your task at hand we might be able to give you an alternative solution, possibly using data frames and data wrangling tools specifically designed to work with structured data.

This topic was automatically closed 21 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.