[英]Drop levels of a factor when droplevels() doesn't work in R
從data.frame D
子集變量t
(它是NULL
的向量)后,我得到一個類因子的對象。
我使用droplevels
刪除級別並獲得NULL
的向量,我想知道為什么我仍然不能實現NULL
的向量?
D <- read.csv("https://raw.githubusercontent.com/izeh/i/master/m.csv", h = T)
L <- split(D, D$study.name) ; L[[1]] <- NULL
t <- lapply(1:length(L), function(i) L[[i]]$t)
droplevels(t[[1]]) ## keep the vector of `NULL`s but drop the levels
## EXPECTED OUTPUT:
[[1]]
[1] NULL NULL NULL NULL NULL NULL
在R中,NULL對象是類似NA的細節。 這篇文章是一個很好的解釋:
要使用NULL值對象創建空向量,很難,因為它是一個長度為0的對象,也許您可以使用NA或使用其他解決方案:
D <- read.csv("https://raw.githubusercontent.com/izeh/i/master/m.csv", h = T)
L <- split(D, D$study.name)
L[[1]] <- NULL # NULL is 0 length, you cancel the first element of your list.
t <- lapply(1:length(L), function(i) L[[i]]$t) # Your try
# 2 solutions :
t <- lapply(1:length(L), function(i) rep(NA, length(L[[i]]$t))) # Replace with NA
t <- lapply(1:length(L), function(i) rep(list(NULL), length(L[[i]]$t))) # Replace with list of NULL
: the result is a list of list with NULL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.