r - Sorting over a list inside a dataframe in R
问题描述
I got a dataframe where a column (in this case, V2
) is a list, like this:
df <- structure(list(V1 = c(1, 2, 3),
V2 = list(c(1, 2, 78, 3), c(9, 4, 78, 8), c(33, 18, 25, 20, 10, 23))),
row.names = c(NA, -3L), class = "data.frame")
# V1 V2
#1 1 1, 2, 78, 3
#2 2 9, 4, 78, 8
#3 3 33, 18, 25, 20, 10, 23
I need to rearrange the elements on each row of V2
. I've tried lapply
ing a sort
over V2
, but can't get it to work.
Here is my desired output:
# V1 V2
#1 1 1, 2, 3, 78
#2 2 4, 8, 9, 78
#3 3 10, 18, 20, 23, 25,33
Any suggestions are very much appreciated!
解决方案
简单地:
df$V2 <- lapply(df$V2, sort)
推荐阅读
- matlab - 在 Matlab 中结合基于索引和逻辑寻址
- android - 为什么单个 dex 文件中有多个类?
- java - Android LinearLayout,设置背景为透明
- node.js - React,从 package.json 代理到 websocket 获取本地机器
- c# - 我该如何解决这个错误?没有给出与所需形式参数相对应的参数
- python - matlab中ordertrack函数的Python等价物
- java - 将 SQL 语句从命令行传递到 Java 代码
- php - 如何通过 application.ini 文件在 zend 框架 1 中设置数据库连接超时
- android - 如何确保在使用说话之前初始化 TextToSpeech?
- c# - 在 Visual Studio 中打开统一代码片段