📌  相关文章
📜  如何从R数据框中删除空行?

📅  最后修改于: 2022-05-13 01:54:48.810000             🧑  作者: Mango

如何从R数据框中删除空行?

数据框可以包含空行,这里的空行不是 NA、NaN 或 0,它的字面意思是绝对没有数据的空行。这样的行显然浪费空间并使数据框不必要地大。本文将讨论如何做到这一点。

为了删除带有空单元格的行,我们在 R 语言中有一个语法,这使得用户可以更轻松地自动删除数据框中尽可能多的空行。

句法:

方法



  • 创建数据框
  • 选择空行
  • 删除那些行
  • 复制结果数据帧
  • 显示数据框

示例 1:

R
gfg <- data.frame(a=c('i','','iii','iv','','vi','','viii','','x'),
                  b=c('I','','III','IV','','VI','','VIII','','X'), 
                  c=c('1','','3','4','','6','','8','','10'), 
                  d=c('a','','c','d','','f','','h','','j'))
  
print('Original dataframe:-')
gfg
  
gfg <- gfg[!apply(gfg == "", 1, all),]
print('Modified dataframe:-')
gfg


R
gfg <- data.frame( A=c('a','','c','','e'),
                   B=c('5','','5','','5'),
                   C=c('1','','1','','1'),
                   D=c('3','','3','','3'),
                   E=c('#','','#','','#'),
                   F=c('@','','@','','@'),
                   H=c('8','','8','','8'))
                     
print('Original dataframe:-')
gfg
  
gfg <- gfg[!apply(gfg == "", 1, all),]
print('Modified dataframe:-')
gfg


输出:

示例 2:

电阻

gfg <- data.frame( A=c('a','','c','','e'),
                   B=c('5','','5','','5'),
                   C=c('1','','1','','1'),
                   D=c('3','','3','','3'),
                   E=c('#','','#','','#'),
                   F=c('@','','@','','@'),
                   H=c('8','','8','','8'))
                     
print('Original dataframe:-')
gfg
  
gfg <- gfg[!apply(gfg == "", 1, all),]
print('Modified dataframe:-')
gfg

输出: