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.
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.
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.
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.
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])])
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With