如何根据给定值更新列表值

问题描述 投票:0回答:1

在下面的

mlist
中,如何将值更新为
character
,当前的值是
"ordered" "factor"

library(tidyverse)
 mlist <- sapply(diamonds,class) 

mlist[['color']] <- 'character'
只能更新其中一个,有没有办法一次性更新全部

mlist[['color']] <- 'character'
r list
1个回答
0
投票

这是我能想到的最快的方法:

# Positions where `mlist` has the value `c("ordered", "factor").
pos <- which(sapply(mlist, function(x) identical(x, c("ordered", "factor"))))
# Change these
mlist[pos] <- "character"
# Result
mlist

请注意,这不会更改数据集的类变量,而只是值是您生成的列表

mlist

© www.soinside.com 2019 - 2024. All rights reserved.