我有以下制表符分隔文件(在变量名下读取我的代码"数据"):
;with cte as (
select
A.ClaimNumber
, C.ActivityDetail
, C.CreatedDateTime as DateClosed
, A.CreatedDate as OpenDate
, datediff(day, convert(date,A.CreatedDate), convert(date, C.CreatedDateTime)) as [Days until closed]
, rn = row_number() over (
partition by A.ClaimNumber
order by c.CreatedDatetime desc /* most recent */
, a.CreatedDate desc /* most recent */
)
from asr.dbo.tblRepairRequestActivityStream C
inner join asr.dbo.tblClaimReason A
on A.ClaimNumber = C.ClaimNumber
where C.ActivityDetail = 'from Ready_For_Pickup to Closed'
and C.CreatedDateTime > '2017-04-16'
and a.Item not in ('emms','detl','wind')
)
select *
from cte
where rn = 1
我希望将其放入以下格式的表格中:
data <- read.csv(text="Species,Salt,Inhibited
C. violaceum,Cadmium nitrate,1
C. violaceum,Cadmium chloride,1
C. violaceum,Cobalt chloride,1
C. violaceum,Cobalt nitrate,1
C. violaceum,Iron (III) chloride,0
C. violaceum,Iron (III) sulfate,0
C. violaceum,Iron (II) sulfate,0
C. violaceum,Manganese chloride,0
C. violaceum,Manganese sulfate,0
C. violaceum,Nickel chloride,0
P. aeruginosa,Cadmium nitrate,1
P. aeruginosa,Cadmium chloride,1
P. aeruginosa,Cobalt chloride,1
P. aeruginosa,Cobalt nitrate,1
P. aeruginosa,Iron (III) chloride,0
P. aeruginosa,Iron (III) sulfate,0
P. aeruginosa,Iron (II) sulfate,0
P. aeruginosa,Manganese chloride,0
P. aeruginosa,Manganese sulfate,0
P. aeruginosa,Nickel chloride,1
S. marcescens,Cadmium nitrate,1
S. marcescens,Cadmium chloride,1
S. marcescens,Cobalt chloride,1
S. marcescens,Cobalt nitrate,1
S. marcescens,Iron (III) chloride,0
S. marcescens,Iron (III) sulfate,0
S. marcescens,Iron (II) sulfate,0
S. marcescens,Manganese chloride,0
S. marcescens,Manganese sulfate,0
S. marcescens,Nickel chloride,1")
等。 (我想包括所有数据,但这里只输入了一小部分数据) 到目前为止,我已设法制作一个表格,在第一列中显示The Salt,并使用以下代码在第二列中显示禁止编号:
Salt No.Inhibited Species.Inhibited
Cadmium nitrate 3 C. violaceum, P. aeruginosa, S. marcescens
Iron (III) chloride 0 None
Nickel chloride 2 P. aeruginosa, S. marcescens
但我不能让被禁止的物种出现在第三栏中。我尝试过使用带有ifelse语句的for循环:
data1 <- aggregate(Inhibited~Salt, data=data, FUN = sum)
但这只会创建第三列,其值为&#34; 1&#34;在每一行。我的教授建议我使用dcast(来自reshape2包)尝试完成这项工作,但我也无法解决这个问题。有人可以给我一些关于创建第三列的方向吗?
答案 0 :(得分:2)
您可以将dplyr
用于此
library(dplyr)
data %>% group_by(Salt) %>%
mutate(keep=Inhibited==1) %>%
summarize(count=sum(keep), Inhibited=paste(Species[keep], collapse=", "))
给出了
Salt count Inhibited
<fctr> <int> <chr>
1 Cadmium chloride 3 C. violaceum, P. aeruginosa, S. marcescens
2 Cadmium nitrate 3 C. violaceum, P. aeruginosa, S. marcescens
3 Cobalt chloride 3 C. violaceum, P. aeruginosa, S. marcescens
4 Cobalt nitrate 3 C. violaceum, P. aeruginosa, S. marcescens
5 Iron (II) sulfate 0
6 Iron (III) chloride 0
7 Iron (III) sulfate 0
8 Manganese chloride 0
9 Manganese sulfate 0
10 Nickel chloride 2 P. aeruginosa, S. marcescens