简体   繁体   English

R中基于行值和类别的条件计算

[英]Conditional calculation in R based on Row values and categories

I have this dataframe:我有这个数据框:

df<-data.frame(a=c("a1","a2","a3","a4","b1","b2","b3","b4","a1","a2","a3","a4","b1","b2","b3","b4"), b=c("x1","x2","x3","total","x1","x2","x3","total", "x1","x2","x3","total","x1","x2","x3","total"), reg=c("A","A","A","A","A","A","A","A","B", "B","B","B","B","B","B","B"), c=c(1:16))

which looks like:看起来像:

    a     b reg  c
1  a1    x1   A  1
2  a2    x2   A  2
3  a3    x3   A  3
4  a4 total   A  4
5  b1    x1   A  5
6  b2    x2   A  6
7  b3    x3   A  7
8  b4 total   A  8
9  a1    x1   B  9
10 a2    x2   B 10
11 a3    x3   B 11
12 a4 total   B 12
13 b1    x1   B 13
14 b2    x2   B 14
15 b3    x3   B 15
16 b4 total   B 16

columns 'a', 'b' and 'reg' are categorical variables. 'a'、'b' 和 'reg' 列是分类变量。 What I want to do is to create a new column which divides x(i), where i=1,2,3 with 'total' (x(i)/total) for each category in reg' and in a' columns.我想要做的是创建一个新列,该列将 x(i),其中 i=1,2,3 与reg' and in a' 列reg' and in的每个类别的 'total' (x(i)/total) 分开。

Can someone help me with this ?有人可以帮我弄这个吗 ?

Assuming your df is ordered like your example .假设您的 df 像您的 example 一样订购。

library(zoo)
df$NEW=df$c
df$NEW[df$b!='total']=NA
df$NEW=na.locf(df$NEW,fromLast=T,na.rm=F)
df$NEW=df$c/df$NEW

df
    a     b reg  c       NEW
1  a1    x1   A  1 0.2500000
2  a2    x2   A  2 0.5000000
3  a3    x2   A  3 0.7500000
4  a4 total   A  4 1.0000000
5  b1    x1   A  5 0.6250000
6  b2    x2   A  6 0.7500000
7  b3    x2   A  7 0.8750000
8  b4 total   A  8 1.0000000
9  a1    x1   B  9 0.7500000
10 a2    x2   B 10 0.8333333
11 a3    x2   B 11 0.9166667
12 a4 total   B 12 1.0000000
13 b1    x1   B 13 0.8125000
14 b2    x2   B 14 0.8750000
15 b3    x2   B 15 0.9375000
16 b4 total   B 16 1.0000000

Base on Op's explanation ,Below are working for the real data of he/she.(From OP)基于Op的解释,以下是他/她的真实数据。(来自OP)

data1$shares<-NA 
id<-which(data1$Occupation=='Total') 
data1$shares[id]<-data1$2014[id]
data1$shares=na.locf(data1$shares,fromLast=T,na.rm=F) 
data1$shares=data1$2014/data1$shares

Just using R base:仅使用 R 基础:

df<-data.frame(a=c("a1","a2","a3","a4","b1","b2","b3","b4","a1","a2","a3","a4","b1","b2","b3","b4"), b=c("x1","x2","x3","total","x1","x2","x3","total", "x1","x2","x3","total","x1","x2","x3","total"), reg=c("A","A","A","A","A","A","A","A","B", "B","B","B","B","B","B","B"), c=c(1:16))

totals <- data.frame(To=df[df$b=='total',4])
totals$from <- c(1, totals$To[1:nrow(totals)-1]+1)
df$NEW = df$c/totals[findInterval(x=df$c, vec=c(rbind(totals$from, totals$to))), 1]
df

Output:输出:

    a     b reg  c       NEW
1  a1    x1   A  1 0.2500000
2  a2    x2   A  2 0.5000000
3  a3    x3   A  3 0.7500000
4  a4 total   A  4 1.0000000
5  b1    x1   A  5 0.6250000
6  b2    x2   A  6 0.7500000
7  b3    x3   A  7 0.8750000
8  b4 total   A  8 1.0000000
9  a1    x1   B  9 0.7500000
10 a2    x2   B 10 0.8333333
11 a3    x3   B 11 0.9166667
12 a4 total   B 12 1.0000000
13 b1    x1   B 13 0.8125000
14 b2    x2   B 14 0.8750000
15 b3    x3   B 15 0.9375000
16 b4 total   B 16 1.0000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM