[英]Drop levels of a factor when droplevels() doesn't work in R
After subsetting variable t
(which is a vector of NULL
s) from my data.frame D
, I get an object of class factor. 从data.frame D
子集变量t
(它是NULL
的向量)后,我得到一个类因子的对象。
I use droplevels
to drop the levels and get a vector of NULL
s, I was wondering why I can't still achieve a vector of NULL
s? 我使用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
In R, NULL objects are specifics like NA. 在R中,NULL对象是类似NA的细节。 This post is a really good explanation : 这篇文章是一个很好的解释:
repeat multiple NULL in R 在R中重复多个NULL
To create an empty vector with NULL value object, it is hard because it is a 0 length object, maybe you could use NA or use the other solution : 要使用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.