我的数据框df = data.frame(c("2012","2012","2012","2013"),
c("AAA","BBB","AAA","AAA"),
c("X","Not-serviced","X","Y"))
colnames(df) = c("year","type","service_type")
包含不同的列。
df2
我需要获取以下数据框year type num_serviced num_notserviced num_total
2012 AAA 2 0 2
...
:
type
因此,我需要按year
和Not-serviced
对数据进行分组,然后计算X
和所有其他频率,例如Y
,temp = aggregate(df,
list(type = dat_human_errors$type,
year = dat_human_errors$year),
FUN = function(x){NROW(x)})
等(假设已提供服务)。
这是我的代码,计算总计:
num_serviced
但是如何创建num_notserviced
和if type=="Not-serviced" num_notserviced++ else num_serviced++
?应该有一些IF-THEN规则,如<div class="portfolioimages"><img src="image.png"/></div>
<div class="portfolioimages"><img src="image1.png"/></div>
<div class="portfolioimages"><img src="image2.png"/></div>
<div class="portfolioimages"><img src="image3.png"/></div>
<div class="portfolioimages"><img src="image4.png"/></div>
<div class="portfolioimages"><img src="image5.png"/></div>
<div class="portfolioimages"><img src="image6.png"/></div>
<div class="portfolioimages"><img src="image7.png"/></div>
<div class="portfolioimages"><img src="image8.png"/></div>
。
答案 0 :(得分:3)
我们可以尝试使用data.table
。转换&#39; data.frame&#39;到&#39; data.table&#39; (setDT(df)
),按年份&#39;分组&#39;键入&#39;,获取sum
逻辑向量,最后得到总数。
library(data.table)
setDT(df)[, .(num_serviced = sum(service_type != "Not-serviced"),
num_notserviced = sum(service_type =="Not_serviced")),
.(year, type)][, Total := num_serviced + num_notserviced][]
答案 1 :(得分:2)
您可以使用.ratings_choice {
overflow: visible;
padding: 10px;
position: relative;
width: 180px;
height: 32px;
}
.ratings_stars {
float: left;
padding: 2px;
}
.ratings_vote {
color: green;
}
.ratings_choice {
font: 10px verdana, sans-serif;
margin: 0 auto 40px auto;
width: 180px;
}
,
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="iva_ratings_choice">
<div id="r1" class="ratings_choice">
<a class="star_1 ratings_stars" data-id="1"><i class="fa fa-star-o fa-3x"></i></a>
<a class="star_2 ratings_stars" data-id="2"><i class="fa fa-star-o fa-3x"></i></a>
<a class="star_3 ratings_stars" data-id="3"><i class="fa fa-star-o fa-3x"></i></a>
<a class="star_4 ratings_stars" data-id="4"><i class="fa fa-star-o fa-3x"></i></a>
<a class="star_5 ratings_stars" data-id="5"><i class="fa fa-star-o fa-3x"></i></a>
</div></div>
<input type="hidden" name="client_ratings" id="client_ratings">
答案 2 :(得分:1)
崩溃数据的最快方法是使用数据包data.table
library(data.table)
df = data.frame(year = c("2012","2012","2012","2013"),
type = c("AAA","BBB","AAA","AAA"),
service_type= c("X","Not-serviced","X","Y"))
dt <- data.table(df)
dt<- dt[,list(num_serviced= sum(service_type!="Not-serviced"), num_notserviced= sum(service_type=="Not-serviced")), by=c("year", "type")]
dt$num_total <- dt$num_serviced + dt$num_notserviced
#if you need to go back to dataframe:
df <- data.frame(dt)
df
year type num_serviced num_notserviced num_total
1 2012 AAA 2 0 2
2 2012 BBB 0 1 1
3 2013 AAA 1 0 1