在R中对数据帧内的列表进行排序

在R中对数据帧内的列表进行排序,r,list,sorting,lapply,R,List,Sorting,Lapply,我得到了一个数据帧,其中一列(在本例中,V2)是一个列表,如下所示: 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
)是一个列表,如下所示:

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
非常感谢您的任何建议

简单地说:


df$V2在
tidyverse
中,我们可以使用
map

library(purrr)
library(dplyr)
df <- df %>%
    mutate(V2 = map(V2, sort))
df
#  V1                     V2
#1  1            1, 2, 3, 78
#2  2            4, 8, 9, 78
#3  3 10, 18, 20, 23, 25, 33
库(purrr)
图书馆(dplyr)
df%
变异(V2=映射(V2,排序))
df
#V1 V2
#1  1            1, 2, 3, 78
#2  2            4, 8, 9, 78
#3  3 10, 18, 20, 23, 25, 33

您是否搜索过可能的解决方案?你能告诉我们你到底做了什么吗?你确定
lappy
V2
上进行
排序不起作用吗
library(purrr)
library(dplyr)
df <- df %>%
    mutate(V2 = map(V2, sort))
df
#  V1                     V2
#1  1            1, 2, 3, 78
#2  2            4, 8, 9, 78
#3  3 10, 18, 20, 23, 25, 33