我有一个数据框,其中包含有关几个类别和一些相关变量的信息。它的形式为:
ID category sales score
227 A 109 21
131 A 410 24
131 A 509 1
123 B 2 61
545 B 19 5
234 C 439 328
654 C 765 41
我想要做的是能够引入两个新列salesRank
和scoreRank
,我找到每个类别的项目索引,如果它们是sales
排序的,分别为score
。我可以像这样解决一般情况:
dF <- dF[order(-dF$sales),]
dF$salesRank<-seq.int(nrow(dF))
但是这并没有考虑到这些类别,到目前为止我只是通过分解数据框来解决这个问题。我想要的将导致以下结果:
ID category sales score salesRank scoreRank
227 A 109 21 3 2
131 A 410 24 2 1
131 A 509 1 1 3
123 B 2 61 2 1
545 B 19 5 1 2
234 C 439 328 2 1
654 C 765 41 1 2
非常感谢!
答案 0 :(得分:3)
尝试:
library(dplyr)
df %>%
group_by(category) %>%
mutate(salesRank = row_number(desc(sales)),
scoreRank = row_number(desc(score)))
给出了:
#Source: local data frame [7 x 6]
#Groups: category
#
# ID category sales score salesRank scoreRank
#1 227 A 109 21 3 2
#2 131 A 410 24 2 1
#3 131 A 509 1 1 3
#4 123 B 2 61 2 1
#5 545 B 19 5 1 2
#6 234 C 439 328 2 1
#7 654 C 765 41 1 2
来自帮助:
row_number()
:相当于rank(ties.method = "first")
min_rank()
:相当于rank(ties.method = "min")
desc()
:将矢量转换为将按降序排序的格式
顺序。正如@thelatemail指出的那样,对于这个特定的数据集,您可能希望使用min_rank()
代替row_number()
,这将更恰当地说明销售/得分的关系:
> row_number(c(1,2,2,4))
#[1] 1 2 3 4
> min_rank(c(1,2,2,4))
#[1] 1 2 2 4
答案 1 :(得分:3)
在基数R中使用ave
rank
(-
将排名从低到高反转为从高到低):
dF$salesRank <- with(dF, ave(-sales, category, FUN=rank) )
#[1] 3 2 1 2 1 2 1
dF$scoreRank <- with(dF, ave(-score, category, FUN=rank) )
#[1] 2 1 3 1 2 1 2
答案 2 :(得分:1)
我只有public static string ProcessRequestSOAP()
{
string result = "";
try
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(SOAPXML_Request); // This is the SOAP xml document that is generated when calling web service. You have to manually create this.
string url = webServiceURL; // External Web Service URL
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.Credentials = new NetworkCredential(userName,password); // Username/password to call web service if required
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelop.Save(stream);
}
//get response from server
WebResponse response = webRequest.GetResponse();
//read response stream
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
if (reader != null)
{
result = System.Xml.Linq.XDocument.Load(reader).ToString();
}
else
{
result = null;
}
}
}
catch (SoapException ex)
{
result = new ExceptionXElement(ex, false).ToString();
}
catch (Exception ex)
{
result = new ExceptionXElement(ex, false).ToString();
}
return result;
}
的基础R解决方案。
tapply