how to transform many columns of a data frame in r

Malta

I have a simple data frame

d <- data.frame(var1=c(5,5,5),var1_c=c(5,NA,6),var2 =c(6,6,6),var2_c = c(8,6,NA))

with a lots of lines, and a lots of variables, all labeled "varXXX" and "varXXX_c", and I want that everytimes there's a NA in a varXXX_c to replace the NA with the value in the varXXX variable. In short, I want to do :

d[is.na(d$var1_c),"var1_c"] <- d$var1[is.na(d$var1_c)]

but try to find a better way to do this that copy paste and change "1" with the number of the variable.

I would rather find a solution in base R or dplyr, but would be grateful for any help !

akrun

We can use grep to find the column names that start with var followed by numbers (\\d+) followed by _ and followed by c. Similarly, we have another set of logical index for var followed by one or more numbers (\\d+) till the end of the string ($) and then do the subset of columns based on the index and change the NA values (is.na(d[i1])) to the corresponding elements in 'd[i2]`.

i1 <- grepl("var\\d+_c", names(d))
i2 <- grepl('var\\d+$', names(d))
d[i1][is.na(d[i1])] <- d[i2][is.na(d[i1])]

NOTE: This is based on the assumption that the columns are in the same order.

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 transform many columns of a data frame in r

From Dev

how to transform columns of a data frame according to the values in a vector in R?

From Dev

How to pair rows in a data frame with many columns using dplyr in R?

From Dev

How to transform a nested json into a data frame in r?

From Dev

Transform data frame rows into columns using duplicate ID in R

From Dev

How to transform a subset columns of an all string data frame in to numeric?

From Dev

How to transform a data.frame of transactions data to sequence data in R?

From Dev

How to select rows from a data frame using a condition from many columns in R

From Dev

R: how to drop rows from a data frame if the rows contain a certain value (for many columns)

From Dev

R: Reorganize data frame with many paired species and abundance columns

From Dev

R How to expand data frame column matrices into data frame columns

From Dev

R How to expand data frame column matrices into data frame columns

From Dev

How to select columns conditionally in a data frame in R

From Dev

How to drop columns in a nested data frame in R?

From Dev

R - transform columns containing key values into multiple columns, in a data.frame object

From Dev

transform all columns in a data frame with dplyr?

From Dev

How to transform data from frame/datatable to matrix in R for a Chord Diagram?

From Dev

Is there a way to transform a three column data frame into two columns while using the same data in R?

From Dev

r data transform separate columns

From Dev

How to add colums to a blank data frame columns by columns in R?

From Dev

R Transform Data Frame and Remove NAs

From Dev

Transform two arrays in to one data frame in R

From Dev

Is it possible to transform a data.frame in a vector in R

From Dev

Data frame columns contains many newline (\n) and its value respectively .How to separate it as new columns and values too

From Dev

Reshape the Columns of Data Frame in R

From Dev

Adding columns to a data frame in R

From Dev

Reshape the Columns of Data Frame in R

From Dev

R: how to check if all columns in a data.frame are the same

From Dev

R how to merge different columns of data frame in one

Related Related

  1. 1

    how to transform many columns of a data frame in r

  2. 2

    how to transform columns of a data frame according to the values in a vector in R?

  3. 3

    How to pair rows in a data frame with many columns using dplyr in R?

  4. 4

    How to transform a nested json into a data frame in r?

  5. 5

    Transform data frame rows into columns using duplicate ID in R

  6. 6

    How to transform a subset columns of an all string data frame in to numeric?

  7. 7

    How to transform a data.frame of transactions data to sequence data in R?

  8. 8

    How to select rows from a data frame using a condition from many columns in R

  9. 9

    R: how to drop rows from a data frame if the rows contain a certain value (for many columns)

  10. 10

    R: Reorganize data frame with many paired species and abundance columns

  11. 11

    R How to expand data frame column matrices into data frame columns

  12. 12

    R How to expand data frame column matrices into data frame columns

  13. 13

    How to select columns conditionally in a data frame in R

  14. 14

    How to drop columns in a nested data frame in R?

  15. 15

    R - transform columns containing key values into multiple columns, in a data.frame object

  16. 16

    transform all columns in a data frame with dplyr?

  17. 17

    How to transform data from frame/datatable to matrix in R for a Chord Diagram?

  18. 18

    Is there a way to transform a three column data frame into two columns while using the same data in R?

  19. 19

    r data transform separate columns

  20. 20

    How to add colums to a blank data frame columns by columns in R?

  21. 21

    R Transform Data Frame and Remove NAs

  22. 22

    Transform two arrays in to one data frame in R

  23. 23

    Is it possible to transform a data.frame in a vector in R

  24. 24

    Data frame columns contains many newline (\n) and its value respectively .How to separate it as new columns and values too

  25. 25

    Reshape the Columns of Data Frame in R

  26. 26

    Adding columns to a data frame in R

  27. 27

    Reshape the Columns of Data Frame in R

  28. 28

    R: how to check if all columns in a data.frame are the same

  29. 29

    R how to merge different columns of data frame in one

HotTag

Archive