本文介绍了将多列合并为一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集,如下所示:
` GN Datum Land AG..GKV. MTCR..GKV. NSGI..GKV. NSGII..GKV. WA..GKV. ML..GKV. Wert
8 11693 2012-01-05 Kenia 5A001f 159700
9 11710 2012-01-05 Indien 1C350.43 18752
10 11749 2012-01-05 Taiwan, Provinz von China 2B350g2 24000
11 11750 2012-01-05 Indien 2B201a1 2B001b2 370296
12 11751 2012-01-05 Serbien 2B201a 7200
13 11752 2012-01-05 Indien 4c 6037
14 11753 2012-01-05 Rumänien 2B001d 7830
15 11755 2012-01-06 China 2B201a1 215987
16 11756 2012-01-06 China 2B201a1 234736
17 11757 2012-01-06 Brasilien 2B231 69900
18 11758 2012-01-06 Brasilien 2B231 139800
19 11788 2012-01-07 Kongo 139800
GN
I唯一的ID。如您所见,对于每一行,变量AG..GKV.
ML..GKV
至多保存一个值,在GN == 11750
的情况下除外。我想将这些变量合并为一个变量,但仍然知道原始变量名是什么,如下所示:
` GN Datum Land variable value Wert
8 11693 2012-01-05 Kenia WA..GKV. 5A001f 159700
9 11710 2012-01-05 Indien AG..GKV. 1C350.43 18752
...
在GN == 11750
中每行有两个值的情况下,它应该如下所示:
` GN Datum Land variable value Wert
11 11750 2012-01-05 Indien NSGII..GKV.
WA..GKV. 2B201a1
2B201a 370296
即"双精度"变量和值应该用新的行分隔符分隔。
UPDATE:还有一个特殊情况:某一行根本没有值。我想保留这些(见上面和下面的GN==19)。在这种情况下,variable
和value
应为空。
` GN Datum Land variable value Wert
19 11788 2012-01-07 Kongo 370296
我不知道如何实现这一点,我已经尝试了reshape
包,但可能是太新手了。
这里有一个dput
(与上面的情况不同,但有三种结果):
structure(list(GN = c(11789L, 11790L, 11791L, 11793L, 11794L,
11795L, 11796L), Datum = structure(c(15355, 15355, 15355, 15355,
15355, 15356, 15356), class = "Date"), Land = c("China", "Israel",
"Pakistan", "Iran, Islamische Republik", "Hong Kong", "Südafrika",
"Lettland"), AG..GKV. = c("2B350d4", "", "", "", "", "", ""),
MTCR..GKV. = c("", "", "", "", "", "", ""), NSGI..GKV. = c("",
"", "", "", "", "", ""), NSGII..GKV. = c("", "", "", "",
"", "", "2B201a1"), WA..GKV. = c("", "7A002a+b", "", "",
"3A001a2c", "6A003b4", "2B001b2"), ML..GKV. = c("", "", "",
"", "", "", ""), Wert = c(63720, 25672, 1608000, 10738, 202500,
13500, 374873)), .Names = c("GN", "Datum", "Land", "AG..GKV.",
"MTCR..GKV.", "NSGI..GKV.", "NSGII..GKV.", "WA..GKV.", "ML..GKV.",
"Wert"), row.names = 49:55, class = "data.frame")
推荐答案
这里有一个带有tidyr和dplyr的选项:
library(dplyr)
library(tidyr)
DF %>% gather(Variable, Value, AG..GKV.:ML..GKV.) %>%
filter(Value != "") %>% group_by(GN, Datum, Land) %>%
summarise_each(funs(paste(unique(.), collapse = "
")))
#Source: local data frame [11 x 6]
#Groups: GN, Datum
#
# GN Datum Land Wert Variable Value
#1 11693 2012-01-05 Kenia 159700 WA..GKV. 5A001f
#2 11710 2012-01-05 Indien 18752 AG..GKV. 1C350.43
#3 11749 2012-01-05 Taiwan, Provinz von China 24000 AG..GKV. 2B350g2
#4 11750 2012-01-05 Indien 370296 NSGII..GKV.
WA..GKV. 2B201a1
2B001b2
#5 11751 2012-01-05 Serbien 7200 NSGII..GKV. 2B201a
#6 11752 2012-01-05 Indien 6037 ML..GKV. 4c
#7 11753 2012-01-05 Rumänien 7830 WA..GKV. 2B001d
#8 11755 2012-01-06 China 215987 NSGII..GKV. 2B201a1
#9 11756 2012-01-06 China 234736 NSGII..GKV. 2B201a1
#10 11757 2012-01-06 Brasilien 69900 NSGII..GKV. 2B231
#11 11758 2012-01-06 Brasilien 139800 NSGII..GKV. 2B231
请注意,这将仅创建由""分隔的多个条目"如果它们是唯一的,我想这就是您想要做的。编辑操作后评论:
对于您的特殊情况,请尝试以下代码:
DF %>% gather(Variable, Value, AG..GKV.:ML..GKV.) %>%
group_by(GN, Datum, Land) %>% filter(if(all(Value == "")) row_number() == 1 else Value != "") %>%
summarise_each(funs(paste(unique(.), collapse = "
")))
解释:
gather
将数据从宽格式重塑为长格式(使用列AG..GKV至ML..GKV)- 然后我们按GN、基准面和土地对数据进行分组
- 筛选条件为:如果所有值条目都是空字符串,则检查行号是否为1(即仅返回此类组的第一行),否则(如果至少一个或多个值条目为非空字符串)仅返回不为空的行
- 然后,在每列(分组列除外)上,将每个组的所有唯一值汇总为单个字符串,以
这篇关于将多列合并为一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!