Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中合并列_R_Merge - Fatal编程技术网

在R中合并列

在R中合并列,r,merge,R,Merge,我在合并数据框中的列时遇到问题 这就是我的数据帧的外观: HDI.Rank Country X1990 X1995 X2000 X2005 X2010 X2011 X2012 X2013 1 171 Afghanistan 121.3 103.0 94.5 84.0 75.3 73.6 72.0 70.2 2 85 Albania 35.1 29.1 23.2

我在合并数据框中的列时遇到问题

这就是我的数据帧的外观:

     HDI.Rank              Country X1990 X1995 X2000 X2005 X2010 X2011 X2012 X2013
    1      171          Afghanistan 121.3 103.0  94.5  84.0  75.3  73.6  72.0  70.2
    2       85              Albania  35.1  29.1  23.2  18.2  14.8  14.2  13.8  13.3
    3       83              Algeria  39.9  36.4  33.9  28.8  23.5  22.8  22.2  21.6
    4       34              Andorra   7.5   5.2   3.9   3.1   2.4   2.4   2.3   2.2
    5      149               Angola 133.4 132.7 128.3 121.5 109.6 107.0 104.3 101.6
    6       58  Antigua and Barbuda  23.4  17.9  13.8  10.6   8.6   8.2   8.0   7.7
我想将所有年份列合并到一个名为year的列中,但我不知道怎么做,因为我仍然需要它来匹配每年的国家和人类发展指数

到目前为止,我已经尝试过使用dplyr,但没有成功

    infant_data %>% unite(X1990, X1995, X2000, X2005, X2010, X2011, X2012, X2013, sep = "", remove = FALSE)

如果有人能帮我的话,我将不胜感激。

idcns
idcns您可以尝试熔化功能:

idcns <- c('HDI.Rank','Country');
`rownames<-`(value=NULL,reshape(
    df, ## input data.frame
    dir='l', ## specify that we want to transform from wide to long format
    idvar=idcns, ## all non-data columns must be identified as id columns
    timevar='year', ## specify the desired time variable column name in the long format
    varying=setdiff(names(df),idcns), ## unfortunately reshape() doesn't know the POE
    split=list(regexp='X',include=T,fixed=T) ## spec how to parse data col name and times
));
##    HDI.Rank     Country year     X
## 1       171 Afghanistan 1990 121.3
## 2        85     Albania 1990  35.1
## 3        83     Algeria 1990  39.9
## 4        34     Andorra 1990   7.5
## 5       149      Angola 1990 133.4
## 6        58 and Barbuda 1990  23.4
## 7       171 Afghanistan 1995 103.0
## 8        85     Albania 1995  29.1
## 9        83     Algeria 1995  36.4
## 10       34     Andorra 1995   5.2
## 11      149      Angola 1995 132.7
## 12       58 and Barbuda 1995  17.9
## 13      171 Afghanistan 2000  94.5
## 14       85     Albania 2000  23.2
## 15       83     Algeria 2000  33.9
## 16       34     Andorra 2000   3.9
## 17      149      Angola 2000 128.3
## 18       58 and Barbuda 2000  13.8
## 19      171 Afghanistan 2005  84.0
## 20       85     Albania 2005  18.2
## 21       83     Algeria 2005  28.8
## 22       34     Andorra 2005   3.1
## 23      149      Angola 2005 121.5
## 24       58 and Barbuda 2005  10.6
## 25      171 Afghanistan 2010  75.3
## 26       85     Albania 2010  14.8
## 27       83     Algeria 2010  23.5
## 28       34     Andorra 2010   2.4
## 29      149      Angola 2010 109.6
## 30       58 and Barbuda 2010   8.6
## 31      171 Afghanistan 2011  73.6
## 32       85     Albania 2011  14.2
## 33       83     Algeria 2011  22.8
## 34       34     Andorra 2011   2.4
## 35      149      Angola 2011 107.0
## 36       58 and Barbuda 2011   8.2
## 37      171 Afghanistan 2012  72.0
## 38       85     Albania 2012  13.8
## 39       83     Algeria 2012  22.2
## 40       34     Andorra 2012   2.3
## 41      149      Angola 2012 104.3
## 42       58 and Barbuda 2012   8.0
## 43      171 Afghanistan 2013  70.2
## 44       85     Albania 2013  13.3
## 45       83     Algeria 2013  21.6
## 46       34     Andorra 2013   2.2
## 47      149      Angola 2013 101.6
## 48       58 and Barbuda 2013   7.7
result=melt(infant_data,id.vars = c("HDI.Rank","Country"),variable.name = "year")[,year:=substring(year,2)]

您可以尝试熔化功能:

result=melt(infant_data,id.vars = c("HDI.Rank","Country"),variable.name = "year")[,year:=substring(year,2)]

看看功能熔化和重塑。您可能会找到您想要的。您可以发布关于Same的示例输出吗?这个问题已经被问了很多次,例如,查看函数melt和Reforme。你可能会找到你想要的。你能发布关于Same的示例输出吗?这个问题已经被问过很多次了,例如,当我使用它作为我的最终代码
rownamesError in
[说句公道话,我可能在你的代码看起来不错的地方出错了,除了
regexp='HDI'
。我不知道你为什么把
'X'
改成
'HDI'
。也许可以试着把它改回去。是的,这很管用,谢谢!我在
[说句公道话,我可能在你的代码看起来不错的地方出错了,除了
regexp='HDI'
。我不知道你为什么把
'X'
改成
'HDI'
。也许可以试着把它改回去。是的,谢谢!