按多列对数据帧进行排序(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
最满意答案
这个:
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如何工作?(How does Mail PHP work?)[2023-06-22]
mail函数使用php.ini中的设置。 可以在Mail Runtime Configuration中找到此配置的详细信息。 可以在php.ini中设置默认值,但您可以使用ini_set覆盖它们。 我打赌你从托管服务器上的PHP脚本发送了邮件。 该服务器可能预先配置了SMTP设置。 如果您在WAMP / LAMP服务器上本地尝试此操作,则必须自己进行此配置,因为PHP无法读取您的Outlook / WhateverMailclient设置。 如评论中所述,您可以自己指定发件人/发件人地址。 SMTP不要求它 ... -
PHP如何工作 - 文学(How does PHP work - literature)[2022-05-21]
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 -
PHP函数无法正常工作(PHP function not work properly)[2023-12-31]
除非使用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 ...
-
与PHP_SELF一起工作(work with PHP_SELF)[2022-12-20]
该错误是说$_POST['action']不存在。 所以,你需要一个条件语句来设置$ act的值。 // Set Default $act = ''; if ( isset($_POST['action'])) ) { $act = $_POST['action']; } switch($act) { case "insert": function_1(); break; default: fuction_2(); ... -
GAE,无法让PHP工作(GAE, can't get PHP to work)[2023-06-03]
您在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 ...
-
PHP变量如何工作?(How PHP variable variables work?)[2023-12-02]
你正在定义$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 ...