首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >组合数据集

组合数据集
EN

Stack Overflow用户
提问于 2022-01-25 14:08:05
回答 2查看 103关注 0票数 0

我应该将https://www.drroyspencer.com/latest-global-temperatures/中的所有四个数据集合并成一个数据集。我没运气就试着把它合并。有什么帮助吗?

到目前为止我尝试过的。

代码语言:javascript
复制
# Lower troposphere data.
lower_trop <- fread("https://www.nsstc.uah.edu/data/msu/v6.0/tlt/uahncdc_lt_6.0.txt", sep = " ")

# Mid-Troposphere data.
mid_trop <- fread("https://www.nsstc.uah.edu/data/msu/v6.0/tmt/uahncdc_mt_6.0.txt", sep = " ")

# Tropopause data.
trop <- fread("https://www.nsstc.uah.edu/data/msu/v6.0/ttp/uahncdc_tp_6.0.txt", sep = " ")

# Lower Stratosphere data.
lower_strat <- fread("https://www.nsstc.uah.edu/data/msu/v6.0/tls/uahncdc_ls_6.0.txt" , sep = " ")

# 
lower_trop1 <- lower_trop[-c(518)]
mid_trop1 <- Mid_trop[-c(518)]
trop1 <- trop[-c(518)]
lower_strat1 <- lower_strat[-c(518)]

# 
lower_trop <- as.data.frame(apply(lower_trop1, 2, as.numeric))
mid_trop <- as.data.frame(apply(mid_trop1, 2, as.numeric))
trop_paus <- as.data.frame((apply(trop1, 2, as.numeric)))
lower_strat <- as.data.frame(apply(lower_strat1, 2, as.numeric))

all<- rbind(lower_trop,mid_trop,trop_paus,lower_strat)
EN

回答 2

Stack Overflow用户

发布于 2022-01-25 14:46:02

  1. Don't忽略警告。您的fread调用是警告

lower_trop <- fread("https://www.nsstc.uah.edu/data/msu/v6.0/tlt/uahncdc_lt_6.0.txt",9月= ") # 100%下载了89265字节.#fread中的警告(“https://www.nsstc.uah.edu/data/msu/v6.0/tlt/uahncdc_lt_6.0.txt",:#在520行的早期停止”)。预期有29个字段,但发现0。比如fill=TRUE和comment.char=。第一条被丢弃的非空线:<>

这是因为这些文件的尾随材料不是“适当的CSV”(就R所关心的而言):

陆洋NoExt SoExt陆地海洋USA48 USA49 1978年12 -0.48 -0.51 -0.47 -0.44 -0.46 -0.42 -0.52 -0.62 -0.50 -0.60 -0.62 -0.37 -0.44 -0.30 -0.46 -0.45 -0.39 -0.68 -0.06 -0.06-0.68-0.68-0.68-0.06-0.06-0.62-0.60-0.60-0.62 -0.59 -0.37-0.44-0.30-0.46 -0.55 -0.45-0.39-0.68-0.68-0.06-0.06-0.45 -0.38 -0.49 -1.29 -1.15 -1.29 1979年1-0.47 -0.64 -0.41 -0.64 -0.86 -0.50 -0.31 -0.34 -0.47 -0.54 -0.45 -0.73 -0.93 -0.55 -0.23 0.12 -0.29 -0.46 -0.95 0.10 -0.16 -0.16 -0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.16-0.3.22-2.42 0.92 ...snip.2021 11 0.08 0.22 0.03 0.11 0.32 -0.02 0.06 -0.00 0.07 0.14 0.35 0.08 0.09 0.31 -0.10 0.02 -0.24 0.06 -0.43 -0.70 -0.72 -0.67 -0.75 0.50 -0.06 -0.29 2021 12 0.21 0.17 0.22 0.19 0.32 0.15 0。12 0.15 0.03 0.30 -0.04 0.40 0.19 0.59 0.20 -0.07 0.25 0.01 -0.25 0.31 -0.42 -0.24 -0.51 1.63 1.42 -0.06年.11 0.16 0.10 0.12 0.16 0.11 0.21 0.17 0.10 0.15 0.09 0.25 0.23 0.27 0.01 0.09 -0.02 0.18 0.18 0.18注:截至2015年4月版本6.0 : 1991-2020年年度周期新参考值:截至2013年6月的5.6版注: 3/13更精确的USA48注: 3/13阿拉斯加州增加了USA49,澳大利亚加入GL 90-90 N,NH 0-90 N,SH 90-0,TRPCS 20-20 N NoExt 20 N-90 N,SoExt 90 0-20,NoPol 60N-90 N,SoPol 90 0-60

没有简单的方法只是“忽略”这些行,所以我建议您非常清楚:声明该警告是预期的,而不是关注这里。你未来的自我(和同事)会感谢你的。您可以使用它来沉默该警告(并允许fread继续正常操作):

lower_trop <- withCallingHandlers( fread("https://www.nsstc.uah.edu/data/msu/v6.0/tlt/uahncdc_lt_6.0.txt",sep = "),警告=函数(W){ if (grepl(“在网上早期停止”,as.character(W)) invokeRestart("muffleWarning") })

  1. R不喜欢非唯一的列名。--我对数据一无所知,但似乎LandOcean是按前面的列GlobeNHSH等分组的。让我们重命名它们,以确保它们是唯一和明确的(不仅仅是数字):

土地<-哪些土地(名称(Lower_trop1) ==“土地”)名称(Lower_trop1)土地<- paste0(名称(Lower_trop1)土地-1,"_",名称(Lower_trop1)土地)名称(Lower_trop1)lands+1 <- paste0(名称(Lower_trop1)土地-1,"_",名称(Lower_trop1)lands+1)头(Lower_trop1)#年份Mo Globe Globe_Land Globe_Ocean NH NH_Land NH_Ocean SH SH_Land SH_Ocean Trpcs Trpcs_Land # 1: 1978年12 -0.48 -0.51 -0.47 -0.44 -0.46 -0.42 -0.52-0.62 -0.50 -0.60 -0.62 # 2: 1979年1 -0.47 -0.64 -0.41 -0.64 -0.86 -0.31 -0.31 -0.34 -0.47 -0.54 # 3: 1979年2 -0.43 -0.56 -0.39-0.47 -0.57 -0.41 -0.39 -0.53 -0.37 -0.36 -0.25 # 4: 1979年3 -0.38 -0.51 -0.33 -0.46 -0.51 -0.44 -0.30 -0.53 -0.26 -0.36 -0.43 # 5: 19794 -0.40 -0.57 -0.34 -0.47 -0.62 -0.37 -0.34 -0.46 -0.35 -0.37 # 6: 1979年5 -0.40 -0.56 -0.52 -0.54 -0.52 -0.27 -0.62-0.19-0.46 -0.55 # 16变量未显示: Trpcs_Ocean,NoExt,NoExt_Land,NoExt_Ocean,SoExt,SoExt_Land,SoExt_Ocean,NoPol,NoPol_Land,NoPol_Ocean,

apply

  • (几乎)从来没有在 data.frame**.**上使用--好吧,几乎从来没有,有合法的时间可以这么做。但是,在默认情况下,当您真正需要matrix保持为data.frame (或至少是list-like)时,它将返回它。使用lapply代替:

lower_trop1[] <- lapply(lower_trop1,as.numeric)

了解这一点以及它如何与框架一起工作的两个技巧:

代码语言:javascript
复制
1. A `data.frame` is just a glorified `list` with some added conditions: all elements should be named uniquely; all elements (typically) must be the same length; it may have row-names, some tools use/honor/keep them, some tools (`dplyr`) ignore or actively remove them. Oh, and the `class` is slightly different. But most things that work on `list`s work just as well on `data.frame`s, column-wise.
代码语言:javascript
复制
2. Even though we're working on columns of a frame, in one sense we're just working on elements of a `list`, _and we want it to stay_ _`list`__-like_, so we should be using `lapply`.
代码语言:javascript
复制
3. Unfortunately, `lapply` is going to return a `list` not a `data.frame`, so the reassignment into `lower_trop1[] <- ...` says to replace the contents of `lower_trop1` without changing its class. There is likely more that can be said here, but know that `frame[] <- lapply(frame, ...)` preserves the `data.frame` properties we need while allowing very simple `lapply`-operations.

从这里开始,rbind应该按预期工作。

FYI,您可以将中间的两个步骤与:

代码语言:javascript
复制
lower_trop1 <- lower_trop[-518,][, lapply(.SD, as.numeric)]

尽管您仍然需要对唯一的列名进行重命名。

票数 1
EN

Stack Overflow用户

发布于 2022-01-25 14:28:22

我猜您想要某种形式的联接(因此从4个数据集中加入列)。这里有一些关于R:https://dplyr.tidyverse.org/reference/mutate-joins.html中不同连接选项的信息

现在,在您的示例中,您有了出现在不同数据集中的YearMo列。所以你可以通过这些列加入数据集。这里是一个左联接的例子(仅用于前两个数据格式):

首先,确保在每个dataframe中都有唯一的列名(您目前没有唯一的名称,因此这个步骤是必要的):

代码语言:javascript
复制
names(lower_trop) <- make.unique(names(lower_trop), sep="_")
names(mid_trop) <- make.unique(names(lower_trop), sep="_")

然后,使用左联接(因此保留左侧数据集中的所有行)将这些行连接起来:

代码语言:javascript
复制
new_df <- left_join(lower_trop, mid_trop, by = c("Year", "Mo"), suffix = c(".lower_trop", ".mid_trop"))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70850065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档