summing vector having cells=inf values
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
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, .N1L), r[1L]),
s=c(rep(NA, .N1L), 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
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

