R - 在保持连接的数据的同时,在数据帧中组合多个列 [英] R - Combining multiple columns together within a data frame, while keeping connected data

查看:135
本文介绍了R - 在保持连接的数据的同时,在数据帧中组合多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对这个问题的答案看了很多,但是我找不到满足我需求或者对R的理解的答案。

So I've looked quite a lot for an answer to this question, but I can't find an answer that satisfies my needs or my understanding of R.

首先,这里有一些代码,只是让你了解我的数据集看起来像

First, here's some code to just give you an idea of what my data set looks like

df <- data.frame("Year" = 1991:2000, "Subdiv" = 24:28, H1 = c(31.2,34,70.2,19.8,433.7,126.34,178.39,30.4,56.9,818.3),
             H2 = c(53.9,121.5,16.9,11.9,114.6,129.9,221.1,433.4,319.2,52.6))             
> df
   Year Subdiv     H1    H2
1  1991     24  31.20  53.9
2  1992     25  34.00 121.5
3  1993     26  70.20  16.9
4  1994     27  19.80  11.9
5  1995     28 433.70 114.6
6  1996     24 126.34 129.9
7  1997     25 178.39 221.1
8  1998     26  30.40 433.4
9  1999     27  56.90 319.2
10 2000     28 818.30  52.6

所以我在这里是一个数据集,其中包含不同年龄的鲱鱼丰富度( Subdiv)随着时间的流逝。 H1代表1岁的鲱鱼。我的真实数据集包含更多的年龄以及更多的区域(以及其他鱼类)。

So what I've got here is a data set containing abundance of herring of different ages in different areas ("Subdiv") over time. H1 stands for herring at age 1. My real data set contains more ages as well as more areas (,and additional species of fish).

我想做什么将不同年龄的丰富度合并成一列,同时保持连接的数据(Year,Subdiv)以及为Age创建一个新列。
喜欢这样:

What I would like to do is combine the abundance of different ages into one column while keeping the connected data (Year, Subdiv) as well as creating a new column for Age. Like so:

       Year Subdiv   Abun   Age
    1  1991     24  31.20    1
    2  1992     25  34.00    1
    3  1993     26  70.20    1
    4  1994     27  19.80    1
    5  1995     28 433.70    1 
    6  1991     24   53.9    2
    7  1992     25  121.5    2
    8  1993     26   16.9    2
    9  1994     27   11.9    2
   10  1995     28  114.6    2

注意:是的,我删除了一些行,但只是不让人满意的屏幕

Note: Yes, I removed some rows, but only to not crowd the screen

我希望这是足够的信息,使其可以理解我需要什么有人帮忙。

I hope this is enough of information for making it understandable what I need and for someone to help.

由于我有更多的鱼类,如果有人想要添加一个Species列的描述,那将是有帮助的。
这里是相同数据的代码,只是重复了sprat(Sn):

Since I have more species of fish, if someone would like to include a description for adding a Species column as well, that would be helpful. Here's code for the same data, just duplicated for sprat (Sn):

df <- data.frame("Year" = 1991:2000, "Subdiv" = 24:28, H1 = c(31.2,34,70.2,19.8,433.7,126.34,178.39,30.4,56.9,818.3),
                 H2 = c(53.9,121.5,16.9,11.9,114.6,129.9,221.1,433.4,319.2,52.6),
                 S1 = c(31.2,34,70.2,19.8,433.7,126.34,178.39,30.4,56.9,818.3),
                 S2 = c(53.9,121.5,16.9,11.9,114.6,129.9,221.1,433.4,319.2,52.6)) 

干杯!

我不认为这个问题的标签应该是无关的,但是如果没有找到适合的标签

I don't think the tags of this question should be unrelated, but if you don't find the tags fitting for my question, go a head and change.

推荐答案

这是一个典型的重塑,然后补充任务,所以你可以:

This is a typical reshape then supplement task so you can:

1)使用reshape2融化您的数据

1) 'Melt' your data with reshape2

library("reshape2")
df.m<-melt(df,id.vars=c("Year","Subdiv"))

2)然后添加adda基于保存以前df列名的变量列的l列l

2) Then add additional columns based on the variable column that holds your previous df's column names

library("stringr")
df.m$Fish<-str_extract(df.m$variable,"[A-Z]")
df.m$Age<-str_extract(df.m$variable,"[0-9]")

我建议您查看重塑功能,因为这些是非常常见的,学习它们将为您节省大量时间
http://www.statmethods.net/management/reshape.html

I recommend you look up the reshape functions as these are very commonly required and learning them will save you lots of time in future http://www.statmethods.net/management/reshape.html

这篇关于R - 在保持连接的数据的同时,在数据帧中组合多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆