R: How to loop over a name-based selection of variables from a dataframe and for each create a new variable containing the column mean of the first?

kommoder_Waran

I have a dataset containing a number of numeric variables whose names all start with "Ranking". For each of these variables, I want to add another variable to the dataset that contains the column mean of the first variable.

So the data look something like this:

| Ranking_blah | Ranking_bleh | 

| --------     | ----------   |

| 1            | 0            |

| 0            | 1            |

| NA           | 0.5          |

and what I want is:

| Ranking_blah | Ranking_bleh | Ranking_blah_mean | Ranking_bleh_mean |

| --------     | ----------   |----------------   |----------------|

| 1            | 0            | 0                 | 0.5            |

| -1           | 1            | 0                 | 0.5            |

| NA           | 0.5          | 0                 | 0.5    

(I am aware this way the mean variables have the same values in all rows, respectively - I need this because the data will be reshaped later)

What I've tried so far:

#getting a list of all ranking variables I want to create a new mean variable from

ranking_variables = names(data)[grepl("Ranking", names(data))]

#creating a new variable for each base variable in the list and setting it to the mean of the respective base variable

data[paste0(ranking_variables, "_mean")] <- do.call(cbind, lapply(data[ranking_variables], function(x) mean(x, na.rm = TRUE)))

The second part is not working, though, it only yields NA values. What am I doing wrong?

harre

An alternative approach is to use dplyr's across:

dat |>
    mutate(across(starts_with("Ranking"), ~ mean(., na.rm = TRUE), .names = "{.col}_mean"))

Output:

# A tibble: 3 × 4
  Ranking_blah Ranking_bleh Ranking_blah_mean Ranking_bleh_mean
         <dbl>        <dbl>             <dbl>             <dbl>
1            1          0                   0               0.5
2           -1          1                   0               0.5
3           NA          0.5                 0               0.5

Data:

tibble(Ranking_blah = c(1,-1,NA), Ranking_bleh = c(0,1,0.5))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a new variable based on the individual mean of some variables from each row?

From Dev

How to create a new column in a pandas dataframe based on values from a loop?

From Dev

R: How to create new variable based on name of other column

From Dev

How to create a new variable based on condition from different dataframe in R

From Dev

How to create a new dataframe for each column by looping over the dataframe columns?

From Dev

Loop over to create new variables from uniform dataframe

From Dev

Create new variables based on other variable and add to dataframe with loop

From Dev

How can I efficiently create a new column in a pandas DataFrame based on another column's rolling mean over a period of 30 days?

From Dev

create new variable name for each loop in a for loop

From Dev

Create new variables from row for each existing variable in pandas dataframe

From Dev

Create new column based on non-numerical variables from several columns in the same dataframe in R

From Dev

R: Create New Dataframe Variable Based on List Element Name

From Dev

How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration?

From Dev

How to subtract previous row from current row in a pandas dataframe to create a new column restarting the process with each name?

From Dev

create new variables based on other variables, with a loop over variables names in R

From Dev

How to create a new categorical variable based on the location of first zero in a column in a long format data using R?

From Dev

In R: How do I create a dataframe name from a string plus a column name plus categorical variable?

From Dev

How to iterate over column values in a dataframe, take the mean, and create a new dataframe?

From Dev

How to create variables for each column iterated over?

From Dev

How to create new variable at the end of each loop iteration in R

From Dev

R: create new column with name coming from variable

From Dev

How to create, name, and populate new column with output Using For loop R

From Dev

Pandas dataframe, how to create a new totals column containing values based on other column

From Dev

How to extract information from a dataframe name and create a column based on it

From Dev

How to create a new variable (column) based on a combination of row values in R?

From Dev

How to save each row to csv in dataframe AND name the file based on the the first column in each row

From Dev

create a new variable based on the name of the dataset in R

From Dev

How to create a new column in a DataFrame and move select data from the first column to the new column

From Dev

Create a new column with partial name from dataframe

Related Related

  1. 1

    How to create a new variable based on the individual mean of some variables from each row?

  2. 2

    How to create a new column in a pandas dataframe based on values from a loop?

  3. 3

    R: How to create new variable based on name of other column

  4. 4

    How to create a new variable based on condition from different dataframe in R

  5. 5

    How to create a new dataframe for each column by looping over the dataframe columns?

  6. 6

    Loop over to create new variables from uniform dataframe

  7. 7

    Create new variables based on other variable and add to dataframe with loop

  8. 8

    How can I efficiently create a new column in a pandas DataFrame based on another column's rolling mean over a period of 30 days?

  9. 9

    create new variable name for each loop in a for loop

  10. 10

    Create new variables from row for each existing variable in pandas dataframe

  11. 11

    Create new column based on non-numerical variables from several columns in the same dataframe in R

  12. 12

    R: Create New Dataframe Variable Based on List Element Name

  13. 13

    How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration?

  14. 14

    How to subtract previous row from current row in a pandas dataframe to create a new column restarting the process with each name?

  15. 15

    create new variables based on other variables, with a loop over variables names in R

  16. 16

    How to create a new categorical variable based on the location of first zero in a column in a long format data using R?

  17. 17

    In R: How do I create a dataframe name from a string plus a column name plus categorical variable?

  18. 18

    How to iterate over column values in a dataframe, take the mean, and create a new dataframe?

  19. 19

    How to create variables for each column iterated over?

  20. 20

    How to create new variable at the end of each loop iteration in R

  21. 21

    R: create new column with name coming from variable

  22. 22

    How to create, name, and populate new column with output Using For loop R

  23. 23

    Pandas dataframe, how to create a new totals column containing values based on other column

  24. 24

    How to extract information from a dataframe name and create a column based on it

  25. 25

    How to create a new variable (column) based on a combination of row values in R?

  26. 26

    How to save each row to csv in dataframe AND name the file based on the the first column in each row

  27. 27

    create a new variable based on the name of the dataset in R

  28. 28

    How to create a new column in a DataFrame and move select data from the first column to the new column

  29. 29

    Create a new column with partial name from dataframe

HotTag

Archive