簡體   English   中英

當droplevels()在R中不起作用時因素的下降級別

[英]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的細節。 這篇文章是一個很好的解釋:

在R中重復多個NULL

要使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM