繁体   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