我有一个包含11个元素的列表cluster_list
,每个元素的长度相同。
> class(cluster_list)
[1] "list"
每个元素都类似于示例元素2:
head(cluster_list[[2]][,1:15])
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
765 t t t c t t a a a a c a t a a
7319 - - - - - - - - - - - - - - -
8335 t t t c t t a a a a c a t a a
7162 - - - - - - - - - - - t c t a
7382 - - - - - - - - - - - - - - -
7244 - - - - - - - - - - - - - - -
我想将列表中的所有11个元素全部从"-"
删除/更改为""
我知道如何在矩阵中做到这一点:
matrix_new <- matrix_old[matrix_old=="-"] <- ""
或者如何删除列表中元素的列:
cluster_list <- lapply(cluster_list, function(x) x[!(names(x) %in% c("X1"))])
但是我无法从列表中删除"-"
。 (我是第一次使用list
和lapply
函数)
任何使它看起来像的建议:
> head(cluster_list[[2]][,1:15])
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
765 "t" "t" "t" "c" "t" "t" "a" "a" "a" "a" "c" "a" "t" "a" "a"
7319 "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
8335 "t" "t" "t" "c" "t" "t" "a" "a" "a" "a" "c" "a" "t" "a" "a"
7162 "" "" "" "" "" "" "" "" "" "" "" "t" "c" "t" "a"
7382 "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
7244 "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
thx K
答案 0 :(得分:3)
我们可以遍历list
,并使用replace
将-
的值更改为空白(''
)
cluster_list_new <- lapply(cluster_list, function(x) replace(x, x== '-', ''))