R 跳过空数据帧并生成输出
#R 跳过空数据帧并生成输出,r,merge,ggplot2,tidyr,rpostgresql,R,Merge,Ggplot2,Tidyr,Rpostgresql,# 库(RPostgreSQL) 图书馆(GG2) 图书馆(gridExtra) 图书馆(比例尺) 图书馆(tidyr) blue.bold.italic.16.text从错误消息判断,导致错误的data.frame既没有行也没有列,它似乎是空的。因此,最简单的方法是检查这种情况,如果data.frame为NULL,则创建一个可以是merge()d和gather()ed的虚拟对象 我要做的(不是说这是最好的方式)是 #为便于循环,请将data.frames放入列表中 l一种替代方法是跳过合并,直
库(RPostgreSQL)
图书馆(GG2)
图书馆(gridExtra)
图书馆(比例尺)
图书馆(tidyr)
blue.bold.italic.16.text从错误消息判断,导致错误的data.frame既没有行也没有列,它似乎是空的。因此,最简单的方法是检查这种情况,如果data.frame为NULL,则创建一个可以是merge()
d和gather()
ed的虚拟对象
我要做的(不是说这是最好的方式)是
#为便于循环,请将data.frames放入列表中
l一种替代方法是跳过合并,直接堆叠数据集。您只需要首先将Ls
列添加到每个单独的数据集中
l2 <- NULL
然后您可以使用,例如,bind_rows
fromdplyr来创建长数据集pd
l1$Ls = "L 1"
l2$Ls = "L 2"
l3$Ls = "L 3"
l4$Ls = "L 4"
这种方法的优点是,绑定的对象之一可以是空的data.frame
或NULL
,并且仍然有效
示例空数据框
:
bind_rows(l1, l2, l3, l4)
Source: local data frame [96 x 3]
hours count Ls
1 2015-06-11 21:00:00 25 L 1
2 2015-06-11 22:00:00 29 L 1
3 2015-06-11 23:00:00 28 L 1
4 2015-06-12 00:00:00 32 L 1
5 2015-06-12 01:00:00 33 L 1
6 2015-06-12 02:00:00 13 L 1
7 2015-06-12 03:00:00 33 L 1
8 2015-06-12 04:00:00 29 L 1
9 2015-06-12 05:00:00 32 L 1
10 2015-06-12 06:00:00 33 L 1
.. ... ... ...
示例NULL
:
l4.2 = data.frame()
bind_rows(l1, l2, l3, l4.2)
Source: local data frame [72 x 3]
hours count Ls
1 2015-06-11 21:00:00 25 L 1
2 2015-06-11 22:00:00 29 L 1
3 2015-06-11 23:00:00 28 L 1
4 2015-06-12 00:00:00 32 L 1
5 2015-06-12 01:00:00 33 L 1
6 2015-06-12 02:00:00 13 L 1
7 2015-06-12 03:00:00 33 L 1
8 2015-06-12 04:00:00 29 L 1
9 2015-06-12 05:00:00 32 L 1
10 2015-06-12 06:00:00 33 L 1
.. ... ... ...
带有“空行”的yr data.frame看起来如何?包含零、NAs,完全没有行?顺便说一句,感谢您以有用的格式提供数据,并提供了良好的解决方案MWE@chanti-如果你能接受答案就好了我想@vaettchen的意思是接受堆栈溢出的答案。要做到这一点,请单击答案的复选标记(位于投票按钮下)。@chanti-我的答案右上角下方有一个按钮,让您有机会将答案标记为“已接受”。这就是我的意思。当然,我更高兴听到你真的运用了我的想法。。。
@ df <- merge(l1,l2, by="hours")
df <- merge(df,l3, by="hours")
df <- merge(df,l4, by="hours")
Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column
pd <- gather(df, 'Ls', 'count', 2:5)
# for easier looping, put your data.frames in a list
l <- list( l1, l2, l3, l4 )
# create a dummy that mimics the structure of your data.frames
dummy <- structure( list( hours = structure( c( Sys.time() ),
class = c( "POSIXct", "POSIXt" ), tzone = ""),
count = c(0)), .Names = c("hours", "count"),
row.names = c(NA, 1L), class = "data.frame")
# check for empty data.frames and replace with dummy (will be NA)
for( i in 1:4 ) if( length( l[[ i ]] ) == 0 ) l[[ i ]] <- dummy
# merge
for( i in 2:4 ) l[[ 1 ]] <- merge( l[[ 1 ]], l[[ i ]],
by = "hours", all = TRUE )
# remove dummy and go back to your code
df <- l[[ 1 ]][ 1:24, ]
colnames( df ) <- c( "hours","L 1","L 2","L 3","L 4" )
l2 <- NULL
l1$Ls = "L 1"
l2$Ls = "L 2"
l3$Ls = "L 3"
l4$Ls = "L 4"
bind_rows(l1, l2, l3, l4)
Source: local data frame [96 x 3]
hours count Ls
1 2015-06-11 21:00:00 25 L 1
2 2015-06-11 22:00:00 29 L 1
3 2015-06-11 23:00:00 28 L 1
4 2015-06-12 00:00:00 32 L 1
5 2015-06-12 01:00:00 33 L 1
6 2015-06-12 02:00:00 13 L 1
7 2015-06-12 03:00:00 33 L 1
8 2015-06-12 04:00:00 29 L 1
9 2015-06-12 05:00:00 32 L 1
10 2015-06-12 06:00:00 33 L 1
.. ... ... ...
l4.2 = data.frame()
bind_rows(l1, l2, l3, l4.2)
Source: local data frame [72 x 3]
hours count Ls
1 2015-06-11 21:00:00 25 L 1
2 2015-06-11 22:00:00 29 L 1
3 2015-06-11 23:00:00 28 L 1
4 2015-06-12 00:00:00 32 L 1
5 2015-06-12 01:00:00 33 L 1
6 2015-06-12 02:00:00 13 L 1
7 2015-06-12 03:00:00 33 L 1
8 2015-06-12 04:00:00 29 L 1
9 2015-06-12 05:00:00 32 L 1
10 2015-06-12 06:00:00 33 L 1
.. ... ... ...
l4.3 = NULL
bind_rows(l1, l2, l3, l4.3)
Source: local data frame [72 x 3]
hours count Ls
1 2015-06-11 21:00:00 25 L 1
2 2015-06-11 22:00:00 29 L 1
3 2015-06-11 23:00:00 28 L 1
4 2015-06-12 00:00:00 32 L 1
5 2015-06-12 01:00:00 33 L 1
6 2015-06-12 02:00:00 13 L 1
7 2015-06-12 03:00:00 33 L 1
8 2015-06-12 04:00:00 29 L 1
9 2015-06-12 05:00:00 32 L 1
10 2015-06-12 06:00:00 33 L 1
.. ... ... ...