logo
down
shadow

Conditionally replace the values in columns to value in another column using dplyr


Conditionally replace the values in columns to value in another column using dplyr

Content Index :

Conditionally replace the values in columns to value in another column using dplyr
Tag : r , By : ack
Date : January 12 2021, 08:33 AM

hope this fix your issue I tried really hard to find an answer to this and I apologize if it's a duplicate. , You can use mutate_at with ifelse:
df %>% mutate_at(vars(starts_with('sample')), funs(ifelse(. == 1, a, .)))

# A tibble: 3 x 3
#      a sample1 sample2
#  <dbl>   <dbl>   <dbl>
#1   0.1     0.0     0.1
#2   0.2     0.2     0.2
#3   0.3     0.3     0.0
df %>% mutate_at(vars(starts_with('sample')), funs(. * a))

# A tibble: 3 x 3
#      a sample1 sample2
#  <dbl>   <dbl>   <dbl>
#1   0.1     0.0     0.1
#2   0.2     0.2     0.2
#3   0.3     0.3     0.0

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

how to conditionally replace values in columns with value of specific column in the same line by Unix and awk commands


Tag : linux , By : Jack
Date : March 29 2020, 07:55 AM
it should still fix some issue I want to conditionally replace values in columns with value of specific column in the same line in one file, by Unix and awk commands. , This seems to do what you're asking for:
% awk 'BEGIN {
     IFS = OFS = "\t"
  }
  {
     for (column = 3; column <= NF; ++column) {
        if ($column == ".") {
            $column = $2
        }
     }    
     print 
  }         
' test.tsv
1       A       A       C       A
2       C       T       C       T
3       T       C       C       T

Using dplyr to conditionally replace values in a column


Tag : r , By : Jody Bannon
Date : March 29 2020, 07:55 AM
wish helps you I have an example data set with a column that reads somewhat like this: , Assuming your data frame is dat and your column is var:
dat = dat %>% mutate(candy.flag = factor(ifelse(var == "Candy", "Candy", "Non-Candy")))

Conditionally replace values in one column with values from another column using dplyr


Tag : r , By : user121501
Date : March 29 2020, 07:55 AM
Does that help With replace, the lengths should be the same, so we need to subset the Other as well with the logical expression
data %>%
    mutate(X25 = replace(X25, X25 == "Other", Other[X25=="Other"]))
data %>%
     mutate(X25 = case_when(X25=="Other"~ Other,
                            TRUE ~ X25))
data %>%
    mutate(X25 = ifelse(X25 == "Other", Other, X25))

R, dplyr, Conditionally change values in multiple columns based on one conditional column in R


Tag : r , By : user150694
Date : March 29 2020, 07:55 AM
it should still fix some issue Given the following data frame: , Do you mean this?
cols <- c("a", "b", "c")
df[is.na(df$d) | df$d == 0, cols] <- NA
df
#   a  b  c  d
#1 NA NA NA NA
#2  2  3  4  1
#3  3  4  5  1
#4 NA NA NA  0
#5 NA NA NA  0
library(dplyr)
df %>% mutate_at(vars(a:c), funs(ifelse(is.na(d) | d == 0, NA, .)))
#   a  b  c  d
#1 NA NA NA NA
#2  2  3  4  1
#3  3  4  5  1
#4 NA NA NA  0
#5 NA NA NA  0

Using dplyr to aggregate data, with column values being conditionally aggregated, dependant on value in other columns


Tag : r , By : Puneet Madaan
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have the following data frame: , You could use summarise after grouping by individual and treat
library(dplyr)
df %>%
   group_by(individual, trait)  %>%
   summarise(flag.1 = first(flag.1), 
             flag.2 = first(flag.2),
             quantity.year.1 = sum(quantity[year == 1]), 
             quantity.year.2 = sum(quantity[year == 2]))

#  individual trait  flag.1 flag.2  quantity.year.1 quantity.year.2
#  <fct>      <fct>  <fct>  <fct>            <int>           <int>
#1 ind.1      blue   1      0                    0              10
#2 ind.2      green  0      0                   13              62
#3 ind.3      yellow 1      1                    9              14
library(dplyr)
library(tidyr)

df %>%
  gather(key, value, quantity) %>%
  group_by(individual, trait, year) %>%
  summarise(sum = sum(value)) %>%
  mutate(year = paste0("sum_quantity", year)) %>%
  spread(year, sum, fill = 0)
Related Posts Related QUESTIONS :
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • Generate a frequency dataframe out of an input dataframe
  • Why manual autocorrelation does not match acf() results?
  • Merge 3 dataframes which are different to each other
  • remove adjacent duplicates from string
  • How to change the position of stacked stacked bar chart in ggplot in R?
  • How to divide each of a range a variables by a second range of variables in R
  • Why do I need to assemble vector before scaling in Spark?
  • How to select individuals which appear in multiple groups?
  • How can I fill columns based on values in another column?
  • 32 bit R and 64 bit R: output differs
  • Remove a single backslash in paste0 output
  • ggplot2 different label for the first break
  • TSP in R, with given distances
  • How to find the given value from the range of values?
  • Solution on R group by issue _ multiple combination
  • Transform multiple columns with a function that uses different arguments per column
  • How can I parse a string with the format "1/16/2019 1:24:51" into a POSIXct or other date variable?
  • How to plot a box plot in R for outlier detection for a huge number of rows?
  • How to change column name according to another dataframe in R?
  • `sjPlot::tab_df()`--how to set the number of decimal places?
  • time average for specific time range in r
  • joining dataframes by closest time and another key in r
  • How to create nested for loop for a certain range
  • New category based on sequence of date ranges
  • how to extract formula from coxph model summary in R?
  • add row based on variable condition in R
  • Generating the sequence 111122222333334
  • Unable to use has_goog_key() in R
  • how to multiply each row with a scaler in corresponding column?
  • R is not recognizing levels of a factor as the same. Is there a way to do this?
  • Calculating mean of replicate experiment result values in a column based on multiple columns using R
  • Best method to extract the first instance of a string between specified keywords using data.table
  • ignore optional combination of alphanumeric characters in str_extract
  • Why tracemem shows two copies when modification occurs inside function body?
  • Can't use mppm on multitype point patterns
  • How to move selected matrix rows to top of matrix based on a selection vector of row names
  • Combining expressions with a common operator
  • Passing string through multiple filters for matching
  • Convert two columns in R to rows of unique occurrence
  • How to create a dataframe using a function based on user-input?
  • How to access the visited vertices in a given shortest path using R igraph
  • Differences in Unicode character output with print()
  • Extracting Function or Objects from a String and then Piping Them with Magrittr/Dplyr
  • renderUI not evaluated until it is rendered
  • Find the maximum absolute value by row in an R data frame
  • Extracting data from irregular lists using purrr:map()
  • transforming data based on range of column in r
  • Identify and subset rows with some similar information
  • converting character from mongolite to timestamp in R
  • Create list from two vectors with every combo of each
  • Error in running a spread because of unique 'key combinations'; combining rows of data
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com