我在R中有以下形式的数据帧:
> head(data)
Group Score Info
1 1 1 a
2 1 2 b
3 1 3 c
4 2 4 d
5 2 3 e
6 2 1 f
Run Code Online (Sandbox Code Playgroud)
我想在Score使用该max函数的列之后聚合它
> aggregate(data$Score, list(data$Group), max)
Group.1 x
1 1 3
2 2 4
Run Code Online (Sandbox Code Playgroud)
但我还想显示与每个组Info的Score列的最大值相关联的列.我不知道该怎么做.我想要的输出是:
Group.1 x y
1 1 3 c
2 2 4 d
Run Code Online (Sandbox Code Playgroud)
任何提示?
我有一个> 1M行长的字符串数据框:
>head(df)
A B C D
1 S1 S2 U1 U2
2 S1 S2 S2 S1
3 S2 S1 S1 S2
4 S1 M2 U1 S2
5 S1 S1 M2 M1
6 M2 M2 M1 M2
Run Code Online (Sandbox Code Playgroud)
我想确定存在特定字符的所有行(例如,“U”)。到目前为止,我发现的解决方案是有效的,但它们非常慢,例如:
matches <- apply(as.matrix(df), 1, function(x){ sum(grepl("U", x, perl=T)) > 0 })
Run Code Online (Sandbox Code Playgroud)
知道如何改进这个查询吗?谢谢!
我正在尝试在R中合并2个列表。该列表的名称应用于执行连接。这是一个玩具示例:
> list1 <- list(A=c(1,2,3), B=c(2,4,5,6), C=c(1,3))
> list2 <- list(A=c(w,x), B=c(y,z))
Run Code Online (Sandbox Code Playgroud)
最终的合并列表应如下所示:
$w
[1] 1 2 3
$x
[1] 1 2 3
$y
[1] 2 4 5 6
$z
[1] 2 4 5 6
Run Code Online (Sandbox Code Playgroud)
到目前为止,我唯一的想法是将列表转换为数据帧,并使用合并功能。还有什么更简单的事情吗?
非常感谢你的帮助。