Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R dplyr: change the row value of columns having an specific name

Tags:

dataframe

r

dplyr

I have a data frame. One of the columns has string values that I want to use as a condition for the name of the other columns. For example,

df <- data.frame(
  cond=c("a","b"),
  aVal=c(1  , 2),
  bVal=c(3  , 4)
)

I want to check the name of each column in the df row by row, if the colname does not start with cond then I want to set the value of that column to 0. The expected output here will be.

#    cond aVal bVal
# 1    a    1    0
# 2    b    0    4

I am not sure how to do this with R preferably with dplyr.

like image 955
user9224 Avatar asked Aug 24 '18 19:08

user9224


People also ask

How do I change rows and column names for a data frame in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector.

How do I select rows with certain names in R?

By using bracket notation on R DataFrame (data.name) we can select rows by column value, by index, by name, by condition e.t.c. You can also use the R base function subset() to get the same results. Besides these, R also provides another function dplyr::filter() to get the rows from the DataFrame.

How do I select a column with specific names in R?

To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.


2 Answers

Here is a base R option

df[-1] <- df[-1] * t(apply(df, 1, function(x)  substr(names(x)[-1], 1, 1) == x[1]))
df
#   cond aVal bVal
#1    a    1    0
#2    b    0    4

Also a variation of the above would be

df[-1] * (substr(matrix(names(df)[-1][row(df[-1])], 2, 2), 1, 1) == 
               df$cond[col(df[-1])])
like image 98
akrun Avatar answered Sep 29 '22 07:09

akrun


Here is a tidyverse solution. Notice that I used stringsAsFactors = FALSE to create your example data frame for avoiding factor columns. df2 is the final output.

library(tidyverse)

df2 <- df %>%
  gather(Column, Value, -cond) %>%
  mutate(Column2 = str_sub(Column, 1, 1)) %>%
  mutate(Value = ifelse(map2_lgl(cond, Column2, ~str_detect(.y, .x)), Value, 0)) %>%
  select(-Column2) %>%
  spread(Column, Value)
df2
#   cond aVal bVal
# 1    a    1    0
# 2    b    0    4

Data

df <- data.frame(
  cond=c("a","b"),
  aVal=c(1  , 2),
  bVal=c(3  , 4),
  stringsAsFactors = FALSE
)
like image 30
www Avatar answered Sep 29 '22 05:09

www