我有600多个数据帧的列表,这些数据帧没有相同的确切变量类型。有时,该类与所需的类有所不同,例如,在整数之前有一个字母,这使整个列成为一个字符(只是因为一个愚蠢的错字...)我需要做的是确定这些数据框没有所需的变量类并进行修改,因此我可以将所有数据用于不同目的(汇总,分析等)。 (我要求类似的帮助,但要注意How to split a list of data frames based on its column names?此处的列名)
我试图根据所需的变量顺序和类别从主列表创建两个列表。为此,我尝试执行以下操作:
v1 <- c(1:15)
v2 <- c(20:34)
v3 <- c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o")
v3b <- c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o")
df1 <- data.frame(v1, v2, v3)
df2 <- data.frame(v1, v2, v3)
df3 <- data.frame(v1, v2, v3b)
df1[1,2] <- "m3"
mylist <- list(df1, df2, df3)
str(mylist[[1]]) # here you can see the class of each variable and how in df1 the class has been changed from integer to character
d_class <- sapply(mylist[[2]], class) # this is how I get the desired class
# now I try to alternatives to identify the dfs with the undesired structure:
#1
list_a <- list()
list_a <- lapply(mylist, function(x) class(x) == d_class) # do not work
grep('FALSE', list_a)
#2
list_b <- list()
list_b <- split(mylist,sapply(mylist,function(x)
identical(class(x),d_class)))
您可以看到,我总是得到所有的数据帧作为返回,并且即使先前指定了代码,代码也无法识别所需的类。
有人知道这段代码有什么问题吗?
非常感谢您的帮助
答案 0 :(得分:2)
由于它是RPS1='%F{154}%D{%H}%F{155}:%F{156}%D{%M}%F{157}:%F{158}%D{%S}'
中的list
,所以data.frame
是class
。我们需要遍历数据集以获得单个列的类
data.frame
如果我们需要逻辑索引lapply(mylist, function(x) sapply(x, class) == d_class)
来查找与'd_class'中具有相同vector
的那些数据集,请在逻辑向量上用class
包装
all