首页 \ 问答 \ 按多列对数据帧进行排序(Sorting a dataframe by multiple columns)

按多列对数据帧进行排序(Sorting a dataframe by multiple columns)

我想对数据框进行排序,而“名称”列中的团队名称保留在“评级”列中的评级。 例如,ne的评分最高为13.62。 我需要将“ne”和“13.62”分类到第一个位置。 这是我的一些代码:

x <-t(nfl_data)
y <- solve(x)
myfun = function(i) round( (1/13)*(sum(x[,i])) + mean(y[,i]), digits=2 )

ratings = numeric(32)
for (i in 1:32){
    ratings[i] = myfun(i)
}

teams <- c('ari','atl','bal','buf','car','chi','cin','cle','dal',
        'den','det','grn','hou','ind','jac','kc','mia','min','ne',
        'no','nyj','nyg','oak','phi','pit','sd','sea','sf','stl',
        'tb','tn','was')

df <- data.frame(teams,ratings)
df[with(df, order(teams, -ratings)), ] 

这是df的示例输出:

     teams ratings
 1    ari   -3.73
 2    atl    9.46
 3    bal    2.31
 4    buf   -5.46
 5    car   -0.69
 6    chi    7.57
 7    cin    6.69
 8    cle   -4.23

如果我尝试运行有序数据帧代码,我会得到相同的结果。 我究竟做错了什么?


I wanted to sort a data frame while the team names in the "name" column stay with the ratings in the "ratings" column. For example, ne has the highest rating of 13.62. I need both "ne" and "13.62" to be sorted to the first position. Here is some of my code:

x <-t(nfl_data)
y <- solve(x)
myfun = function(i) round( (1/13)*(sum(x[,i])) + mean(y[,i]), digits=2 )

ratings = numeric(32)
for (i in 1:32){
    ratings[i] = myfun(i)
}

teams <- c('ari','atl','bal','buf','car','chi','cin','cle','dal',
        'den','det','grn','hou','ind','jac','kc','mia','min','ne',
        'no','nyj','nyg','oak','phi','pit','sd','sea','sf','stl',
        'tb','tn','was')

df <- data.frame(teams,ratings)
df[with(df, order(teams, -ratings)), ] 

Here is the sample output of df:

     teams ratings
 1    ari   -3.73
 2    atl    9.46
 3    bal    2.31
 4    buf   -5.46
 5    car   -0.69
 6    chi    7.57
 7    cin    6.69
 8    cle   -4.23

I get the same results if I try running the ordered data frame code. What am I doing wrong?


原文:https://stackoverflow.com/questions/20710990
更新时间:2024-01-02 16:01

最满意答案

这个:

if (!$sess_uid == $WallID)
{
    Run Action
}

相当于这个:

if ( (!$sess_uid) == ($WallID))
{
    Run Action
}

当你想要:

if (!($sess_uid == $WallID))
{
    Run Action
}

This:

if (!$sess_uid == $WallID)
{
    Run Action
}

Is equivalent to this:

if ( (!$sess_uid) == ($WallID))
{
    Run Action
}

While you want:

if (!($sess_uid == $WallID))
{
    Run Action
}

相关问答

更多
  • mail函数使用php.ini中的设置。 可以在Mail Runtime Configuration中找到此配置的详细信息。 可以在php.ini中设置默认值,但您可以使用ini_set覆盖它们。 我打赌你从托管服务器上的PHP脚本发送了邮件。 该服务器可能预先配置了SMTP设置。 如果您在WAMP / LAMP服务器上本地尝试此操作,则必须自己进行此配置,因为PHP无法读取您的Outlook / WhateverMailclient设置。 如评论中所述,您可以自己指定发件人/发件人地址。 SMTP不要求它 ...
  • PHP手册中有一节标题为PHP:核心:Zend引擎的黑客指南 http://www.php.net/manual/en/internals2.php There's a section in the PHP Manual entitled PHP at the Core: A Hackers' Guide to the Zend Engine http://www.php.net/manual/en/internals2.php
  • 除非使用eval否则无法像在运行时那样生成条件。 foreach ($allowed_users as $key => $value) { $user .= $_SESSION['role'] ." == ".$value." || "; // Here I'm generating statement to use in If condition } $user = substr(trim($user), 0, -3); 你需要的是in_array函数: function acces ...
  • 一般情况: 会话ID在创建会话时发送给用户。 它存储在一个cookie中(默认称为PHPSESSID ) 该cookie由浏览器发送到服务器与每个请求 服务器(PHP)使用包含session_id的cookie来知道与该用户对应的文件。 会话文件中的数据是$_SESSION的内容,序列化(即表示为字符串 - 具有序列化功能) ; 当文件由PHP加载时,它是未序列化的,以填充$_SESSION数组。 有时,会话ID不存储在cookie中,而是发送在URL中,但现在很少见。 有关更多信息,您可以查看手册的“ 会 ...
  • 请参阅手册 。 如果include文本文件,它将在文档中显示为文本。 include不像copy-paste那样: test.php的 的test.txt echo $test; 上面的例子将输出: **echo $test;** 如果要包含PHP文件,仍需要PHP标记 。 此外,通常括号不include在include之后,如下所示: include ...
  • 该错误是说$_POST['action']不存在。 所以,你需要一个条件语句来设置$ act的值。 // Set Default $act = ''; if ( isset($_POST['action'])) ) { $act = $_POST['action']; } switch($act) { case "insert": function_1(); break; default: fuction_2(); ...
  • 您在contact.php的处理程序中有一个不必要的*,尝试将其更改为: - url: /contact.php script: contact.php 此外,在AppEngine上工作的更常见方式是,不要对脚本使用文件扩展名,因此将处理程序更改为: - url: /contact script: contact.php 然后将链接和表单目的地更改为/ contact而不是/contact.php You have an unnecessary * in your handler for con ...
  • 如果您不添加PHP打开/关闭标记,则注释将为T_INLINE_HTML标记。 查看token_get_all上的示例。 更新: 如果您发布PHP代码nl2br没有意义,因为它会将
    标签添加到代码中。 请尝试以下方法: $input = ''; UPDATE2: 如果源只是HTML,则使用DOM解析器。 请参阅此处的示例: 从html源代码中删除注释 如果源是PHP和HTML的混合,您还需要混合使用tokenizer和DOM解析器。 因此,您将使 ...
  • 这个: if (!$sess_uid == $WallID) { Run Action } 相当于这个: if ( (!$sess_uid) == ($WallID)) { Run Action } 当你想要: if (!($sess_uid == $WallID)) { Run Action } This: if (!$sess_uid == $WallID) { Run Action } Is equivalent to this: if ( (!$sess_ui ...
  • 你正在定义$arr = 'arr'; 然后从字符串'arr'获取第二个字符,而不是类Foo中的数组,这就是为什么你得到'r'( [1]从你的单词中返回第二个字符)。 解决方案? 你应该替换: echo $fooo->$arr[1] . "\n"; 有: echo $fooo->arr[1] . "\n"; 你应该收到你想要的输出: 'I am B.' You are defining $arr = 'arr'; and then getting the second character from th ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。