简体   繁体   English

使用R脚本获取列值计数

[英]Get columns values count using r-script

I want to get the count of column values for Prod1 我想获取Prod1的列值计数

S#  Types         Product Name
--  -----         ------------
1   ["A","B","C"] Prod1
2   ["B","C"]     Prod1
3   ["A","C"]     Prod1
4   ["Z"]         Prod2

I want the output for Prod1 in this format(ie Count of each column value) 我希望以这种格式输出Prod1 (即每个列值的计数)

Prod1
-----
A B C
2 2 3

I will use this value to plot a graph. 我将使用此值绘制图形。 Need a pure r-script way to do this without any additional libraries (cause i will use this in PowerBI). 需要一种纯r-script方式来执行此操作,而无需任何其他库(因为我将在PowerBI中使用它)。

library(tidyverse) 
dat%>%
     group_by(Product_Name)%>%
     mutate(Types=str_extract_all(Types,"\\w"))%>%
     summarise(s=list(table(unlist(Types))))%>%
     unstack(s~Product_Name)
$Prod1

A B C 
2 2 3 

$Prod2

Z 
1 

Using base R: 使用基数R:

a=transform(dat,Types=gsub("[^,A-Za-z0-9]","",Types))
b=aggregate(Types~Product_Name,a,function(x)table(unlist(strsplit(unlist(x),","))))
unstack(rev(b))

$Prod1

A B C 
2 2 3 

$Prod2

Z 
1 

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

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