简体   繁体   English

如何从R中的单个CSV文件读取2个不同的数据帧?

[英]How to read 2 different dataframes from single CSV file in R?

Suppose we have 2 dataframes like this in single CSV file as: 假设我们在单个CSV文件中有2个像这样的数据框,如下所示:

Name C1 C2 C3 C4

aa   1   2  3  4

bb   3   4  6  5

cc  10   2  5  6

TT  44   2  2  6

#

Name C1 C2 C3 C4

aa   1   2  3  4

bb   3   4  6  5

cc  10   2  5  6

TT  44   2  3  6

My actual requirement is to read these 2 dataframes in 2 different dataframe variables, I want to analyze these 2 dataframes and plot accordingly, please help me in this regard. 我的实际要求是读取2个不同数据帧变量中的这2个数据帧,我想分析这2个数据帧并作相应的绘图,请在这方面帮助我。

Here's an example 这是一个例子

writeLines(
con = tf <- tempfile(fileext = ".csv"), 
text = "Name C1 C2 C3 C4
aa 1 2 3 4
bb 3 4 6 5
cc 10 2 5 6
TT 44 2 2 6
#
Name C1 C2 C3 C4
aa 1 2 3 4
bb 3 4 6 5
cc 10 2 5 6
TT 44 2 3 6")

txt <- readLines(tf)
sep <- grep("^#", txt)[1]
df1 <- read.table(text = txt[1:(sep-1)], header = TRUE)
df1$part <- "1"
df2 <- read.table(text = txt[(sep+1):length(txt)], header = TRUE)
df2$part <- "2"

library(tidyr)
library(dplyr)
library(ggplot2)
bind_rows(df1, df2) %>% 
  gather(var, val, -part, -Name) %>% 
  ggplot(aes(x = Name, y = val, fill = var)) + 
  geom_col() + 
  facet_wrap(~part)

在此处输入图片说明

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

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