logo
down
shadow

R - how to apply output of ifelse(str_detect ...) to whole group


R - how to apply output of ifelse(str_detect ...) to whole group

By : Zwiterrion Fritzwagg
Date : October 18 2020, 08:10 PM
wish helps you I'm trying to flag all instances in a group if one variable contains the values "PICU" or "CCCU" (or both). , We may do
code :
df %>% select(key, Unit) %>%
  group_by(key) %>% mutate(ICU = 1 * any(c("PICU", "CCCU") %in% Unit))
# A tibble: 21 x 3
# Groups:   key [7]
#      key Unit    ICU
#    <int> <chr> <dbl>
#  1     1 7A        1
#  2     2 2B        1
#  3     3 CCCU      1
#  4     4 PICU      1
#  5     5 7A        1
#  6     6 2B        1
#  7     7 CCCU      1
#  8     1 PICU      1
#  9     2 7A        1
# 10     3 2B        1
# ... with 11 more rows


Share : facebook icon twitter icon
R: split on one column, apply function on each group and display all columns from each group in the output

R: split on one column, apply function on each group and display all columns from each group in the output


By : Hari Kolasani
Date : March 29 2020, 07:55 AM
seems to work fine I have a data set like this:` , With ddply you can use colwise
code :
library(plyr)
ddply(data, .(seq, desc, id), colwise(median))
#    seq desc  id   sample1 sample2 sample3
#1  atg   pq  12 0.5000388     2.5     2.5
#2 atgc  pqr 123 2.0000000     2.0     2.0
aggregate(.~seq+desc+id, data, median)
#   seq desc  id   sample1 sample2 sample3
#1  atg   pq  12 0.5000388     2.5     2.5
#2 atgc  pqr 123 2.0000000     2.0     2.0
library(data.table)
setDT(data)[, 4:6 := lapply(.SD, as.numeric), .SDcols=4:6][,
                            lapply(.SD, median), .(seq, desc, id)]
#    seq desc  id   sample1 sample2 sample3
#1: atgc  pqr 123 2.0000000     2.0     2.0
#2:  atg   pq  12 0.5000388     2.5     2.5
Using ifelse Within apply

Using ifelse Within apply


By : Nilankur
Date : March 29 2020, 07:55 AM
I wish this help you You want to check if any of the variables in a row are 0, so you need to use any(x==0) instead of x == 0 in the ifelse statement:
code :
apply(data, 1, function(x) {ifelse(any(x == 0), NA, length(unique(x)))})
# [1]  1 NA  2
(data <- data.frame(a=c(1, 2, 3), b=c(1, 0, 1)))
#   a b
# 1 1 1
# 2 2 0
# 3 3 1
dplyr mutate stringr str_detect with multiple conditional arguments and corresponding output

dplyr mutate stringr str_detect with multiple conditional arguments and corresponding output


By : Pluswindows
Date : March 29 2020, 07:55 AM
it should still fix some issue The functions you designed (dot3dot3split and dot3split) are not able to vectorize the operation. For example, if there are more than one elements, only the first one is returned. That may cause some problems.
code :
dot3dot3split(c("xxxxx.x...Alpha...Keep.1", "xxxxx.x...Alpha..Keep.2"))
# [1] "Keep.1" 
df <- data.frame(KPI = c("xxxxx.x...Alpha...apples",
                         "xxxxx.x...Alpha..bananas",
                         "Bravo...oranges",
                         "Bravo...grapes",
                         "xxxxx...Charlie...cherries",
                         "xxxxx...Charlie...guavas"))

library(dplyr)
library(stringr)

df1 <- df %>%
  mutate_if(is.factor, as.character) %>%
  mutate(KPI.v2 = str_extract(KPI, "[A-Za-z]*$"))
df1
#                          KPI   KPI.v2
# 1   xxxxx.x...Alpha...apples   apples
# 2   xxxxx.x...Alpha..bananas  bananas
# 3            Bravo...oranges  oranges
# 4             Bravo...grapes   grapes
# 5 xxxxx...Charlie...cherries cherries
# 6   xxxxx...Charlie...guavas   guavas
How to group by a column in pandas and apply a ifelse based on column values

How to group by a column in pandas and apply a ifelse based on column values


By : Ananth Murthy
Date : March 29 2020, 07:55 AM
hop of those help? I believe need numpy.where with DataFrameGroupBy.shift:
code :
shifted = dat.groupby('i_n')['a_q'].shift().fillna(0)
dat['p_q'] = np.where(dat['m_b_r'] == 1, dat['a_q'], dat['o_q'] - shifted)
print (dat)
  i_n  m_b_r  o_q  a_q  p_q
0   a      0    1    1  1.0
1   b      1    8    5  5.0
2   b      0    8   15  3.0
3   d      0    1    1  1.0
4   e      0    1   57  1.0
5   f      0    1    1  1.0
6   g      0    1    5  1.0
7   h      1    2    1  1.0
8   h      0    2    1  1.0
9   i      0    1    1  1.0
def f(x):
    x['p_q'] = np.where(x['m_b_r'] == 1, x['a_q'], x['o_q'] - x['a_q'].shift().fillna(0))
    return x

df = dat.groupby('i_n').apply(f)
print (df)
  i_n  m_b_r  o_q  a_q  p_q
0   a      0    1    1  1.0
1   b      1    8    5  5.0
2   b      0    8   15  3.0
3   d      0    1    1  1.0
4   e      0    1   57  1.0
5   f      0    1    1  1.0
6   g      0    1    5  1.0
7   h      1    2    1  1.0
8   h      0    2    1  1.0
9   i      0    1    1  1.0
Is there a way to group_by a variable, str_detect in each group and store that result in a new column?

Is there a way to group_by a variable, str_detect in each group and store that result in a new column?


By : crojas.imx
Date : March 29 2020, 07:55 AM
Any of those help Your current use of ifelse doesn't do anything: you take the output of str_detect(), which is TRUE/FALSE, and convert it into TRUE/FALSE. To expand the result out to the entire group, you can use any:
code :
library(dplyr)
library(stringr)

df %>%
    group_by(A) %>%
    mutate(yes_in_group = any(str_detect(B, 'yes')))
Related Posts Related Posts :
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • ROC curve from train/test set in caret R package
  • Random Forest for a mixture of categorical,numeric and "unwanted" variables which include missing values
  • extract certain data from multiple excel files with R
  • Matrix with counts of wins and losses between methods in R
  • Grouping string variables from a dataframe by best string match to make subsets
  • Reorder does not work after adding second geom_points
  • cover POS data formate to the one can apply Arules (Apriori)
  • Matching values between data frames based on overlapping dates
  • Grouped bar chart turns into stacked bar chart ggplot
  • R: How to fill in NA Values within a Column based on grouping?
  • Two action buttons, but only the first one, that is written in the server file, works?
  • Barchart grouped by variable both count up to 100 percent
  • Converting time in R to 24 hours
  • R - Web scrapping and downloading multiple zip files and save the files without overwriting
  • Find month and year inside string
  • Append multiple csv files into one file using R
  • Use `purrr::map` with k-means
  • R - 'data' is not an exported object from 'namespace:my_package'
  • Sum vector with number by dinamic intervals without looping
  • Issues with ave function in R: error "cannot allocate vector of size 419 kb."
  • Shiny system call with continuous updates
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org