简体   繁体   English

具有 0 个值的 R 数据框

[英]R data.frame with 0 values

My pos_TAT data frame contains 7 rows but the last one appears only when I do summary because its value is 0.我的 pos_TAT 数据框包含 7 行,但最后一行仅在我进行汇总时出现,因为它的值为 0。

summary(pos_TAT)
Delayed$position.type  Delayed$TAT   
            JF     :1
           S20     :1
           S40     :1
           S60     :1
      Specials     :1
           S70     :1   
         14-19     :0



pos_TAT
  Delayed$position.type Delayed$TAT
1                    JF    45.10965
2                   S20    44.37831
3                   S40    44.18750
4                   S60    45.40698
5              Specials    43.30079
6                   S70    42.44444    

That poses problem if I want to add a column with the count for example as it tells me there's a different number of rows 6, 7.如果我想添加一个带有计数的列,这会带来问题,因为它告诉我第 6 行、第 7 行的行数不同。

I've spent hours looking into that problem but can't find the answer.我花了几个小时研究这个问题,但找不到答案。 Thank you.谢谢你。

This could be possible when there are unused levels for factor columns in the dataset.当数据集中的factor列存在未使用的级别时,这可能是可能的。 Either convert the column to character or if we need to keep it as factor class, then use droplevels or call factor again.将列转换为character或者如果我们需要将其保留为factor类,则使用droplevels或再次调用factor

df2 <- droplevels(pos_TAT)

Or或者

df2 <- transform(pos_TAT, Delayed$position.type= as.character(Delayed$position.type))

The summary would also give the unused levels as the OP showedsummary还将提供未使用的级别,如 OP 所示

summary(pos_TAT[1])
# Delayed$position.type
# JF      :1           
# S20     :1           
# S40     :1           
# S60     :1           
# Specials:1           
# S70     :1           
# 14-19   :0      

data数据

pos_TAT <- structure(list(`Delayed$position.type` = structure(1:6, .Label = c("JF", 
"S20", "S40", "S60", "Specials", "S70", "14-19"), class = "factor"), 
Delayed.TAT = c(45.10965, 44.37831, 44.1875, 45.40698, 43.30079, 
42.44444)), .Names = c("Delayed$position.type", "Delayed.TAT"
), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM