logo
down
shadow

Summing Values of One Vector Conditional on Values of Another Vector


Summing Values of One Vector Conditional on Values of Another Vector

Content Index :

Summing Values of One Vector Conditional on Values of Another Vector
Tag : r , By : codelurker
Date : November 24 2020, 03:41 PM


Comments
No Comments Right Now !

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

Share : facebook icon twitter icon

summing vector having cells=inf values


Tag : matlab , By : cmhudson
Date : March 29 2020, 07:55 AM
I hope this helps you . I need to find out the average of the values , To find how many are Inf:
1) find the infs
Allvalues{1}<inf                    
ans =
     1     1     0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
sum(~(Allvalues{1}<inf))
ans =
     1

Total sum of a numeric vector conditional on values from a character vector


Tag : r , By : rhyhann
Date : March 29 2020, 07:55 AM
Hope this helps x is a numeric vector and y is a character vector, both of the same length. I have to obtain the total sum of x for each character in y. So for example if y has the letters of the alphabet, the algorithm must sum all the values of x that indexed by a "A" and then all the values indexed by a "B", etc. It seems it can be done with the aggregate function but I haven't been able to figure out how. , Test data, in a dataframe for convenience rather than two vectors:
> d = data.frame(x=runif(10),y=sample(LETTERS[1:3],10,TRUE))
> d
            x y
1  0.25927547 B
2  0.95012667 C
3  0.85133149 C
4  0.64658480 B
etc
> tapply(d$x,d$y,sum)
       A        B        C 
1.547225 1.891884 2.666552 
> sum(d$x[d$y=="A"])
[1] 1.547225
> sum(d$x[d$y=="B"])
[1] 1.891884
> sum(d$x[d$y=="C"])
[1] 2.666552
> d=data.frame(x=runif(10),y=sample(LETTERS[1:3],10,TRUE),z=sample(LETTERS[1:3],10,TRUE))
> d
           x y z
1  0.4166217 A C
2  0.5816940 B C
3  0.9915231 A C
4  0.7177323 B C
etc
> aggregate(x~y+z,d,sum)
  y z         x
1 C A 1.6392171
2 B B 0.9389463
3 C B 0.3330299
4 A C 2.3748477
5 B C 1.2994263
> sum(d$x[d$y=="C" & d$z=="A"])
[1] 1.639217
> sum(d$x[d$y=="B" & d$z=="B"])
[1] 0.9389463

Summing values in a vector based on duplicate values in another R


Tag : r , By : user181445
Date : March 29 2020, 07:55 AM
This might help you Let's say I have two vectors below: , Using data.table package:
DT = data.table(r,s)
DT[, `:=`(r=c(rep(NA, .N-1L), r[1L]), 
          s=c(rep(NA, .N-1L), sum(s))
         ), 
by=r]

> DT
#      r   s
#  1: NA  NA
#  2: NA  NA
#  3:  4  43
#  4: NA  NA
#  5: NA  NA
#  6:  6  11
#  7: NA  NA
#  8:  8  97
#  9:  9   9
# 10: NA  NA
# 11:  2 170
# 12:  3  78

Generate a vector: when values in one vector match another vector, input the value of another vector at the same positio


Tag : r , By : Keonne Rodriguez
Date : March 29 2020, 07:55 AM
will help you In your example data df1$outlet is already equal to concordance$outlet at all points. I assume you want something that works even when that's not true, so let's scramble the data first to check that what we do works in all cases:
set.seed(123)
df1 = df1[sample(1:nrow(df1)), , drop = FALSE]
df1 = merge(df1, concordance, by="outlet", sort = FALSE)

Split a vector and summing values


Tag : r , By : Cadu
Date : March 29 2020, 07:55 AM
hope this fix your issue I'm a R newbie. I've got a vector , Starting with this vector...
> vec
 [1] 105  29  41  70  77   0  56  49  63   0 105
> vec == 0
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
> cumsum(vec==0)
 [1] 0 0 0 0 0 1 1 1 1 2 2
> split(vec, cumsum(vec==0))
$`0`
[1] 105  29  41  70  77

$`1`
[1]  0 56 49 63

$`2`
[1]   0 105
> sapply(split(vec, cumsum(vec==0)),sum)
  0   1   2 
322 168 105 
Related Posts Related QUESTIONS :
  • Reset input fields of dynamically generated widgets through insertUI
  • How to get the coordinates that spits out min/max value from the function in R?
  • running t.test() on multiple columns to output tibble
  • Conditionally replace the values in columns to value in another column using dplyr
  • Add count as label to points in geom_count
  • Temporarily Disable Rprofile
  • Select certain region of column for lm
  • Convert multiple rows into one row depending on unique values in another column
  • Issues installing Plotly Dash for R
  • Is there an R function to retrieve values from a matrix of column names?
  • R;Too slow to overate loops for million vectors
  • How to optimize intersect of rows and columns in a matrix?
  • Format and export the output of Mann-Kendall test in R to excel from Rstudio
  • how to calculate cumsum with depreciation in a grouped dataframe?
  • reshape wide to long based on part of column name
  • How to get a hyperlink for the words in a description in an r dataframe?
  • shinymeta works locally but breaks when published to shinyapps.io
  • Deparse and (un)escape quotes
  • Regression table with clustered standard errors in R jupyter notebook?
  • Disaggregate quarterly data to daily data in R keeping values?
  • How to save output to console and file simultaneously in RStudio server?
  • Why does data.table j have a different environment when directly calling mget() vs calling mget() inside another functio
  • scale_fill_viridis_c color bar on a log scale
  • How to change the lab name corresponding to function in ggplot
  • R, filtering for an element in a list in a dataframe cell
  • Extracting only bottom temperature from 4d NetCDF file
  • How to add/wrap lines of text to .tex with .sh script
  • R - building new variables from sequenced data
  • Sum rows values one after the other
  • Nesting ifelse inside summarytools
  • How best to divide different levels of a factor by one another in dataframe in R?
  • Why does my code run multiple times before I type data into the table? How do I make an action button that creates a tab
  • How to impute missing values not at random?
  • Set the y limits of an added average line of a plotly plot
  • how to calculate a new column after grouping with dplyr
  • Extract data from rows creating new columns using R
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • 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
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com