R:添加 x 行,值为 y
我有一个带有三个列的dataframe(df1
):name
,十年
和count
。例如:
Name <- c("a","b","c")
Decade <- c(1810,1850,1900)
Count <- c(2,3,1)
df1 <- data.frame(Name,Decade,Count)
print(df1)
Name Decade Count
1 a 1810 2
2 b 1850 3
3 c 1900 1
我希望创建一个新的DataFrame(df2
),其中列name
和十年
,并在df1中重复值行$ name
和df1 $十年
df1 $ count 。对于DF2中的每个重复行,我希望df2 $十年
增加增量为10。 组合将等于df1 $ name
和df1 $ code
。
在上面的示例中,我所需的输出将是:
> print(df2)
Name Decade
1 a 1810
2 a 1820
3 b 1850
4 b 1860
5 b 1870
6 c 1900
我很想在到目前为止展示我的工作,但是我不知道该如何开始并在Excel中手动进行操作。 [羞耻地悬而未决]
谢谢您提前的时间。
I have a dataframe (df1
) with three columns: Name
, Decade
and Count
. For example:
Name <- c("a","b","c")
Decade <- c(1810,1850,1900)
Count <- c(2,3,1)
df1 <- data.frame(Name,Decade,Count)
print(df1)
Name Decade Count
1 a 1810 2
2 b 1850 3
3 c 1900 1
I wish to create a new dataframe (df2
) with columns Name
and Decade
, with repeating rows of values in df1$Name
and df1$Decade
by df1$Count
. For each repeating row in df2, I wish the df2$Decade
to increase by an increment of 10. The first instance of each df2$Name
and df2$Decade
combination would be equal to df1$Name
and df1$Decade
.
In the above example, my desired output would be:
> print(df2)
Name Decade
1 a 1810
2 a 1820
3 b 1850
4 b 1860
5 b 1870
6 c 1900
I would love to show my workings so far, but I don't know how to start and have been manually doing it in excel. [hangs head in shame]
Thank you for your time in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用
tidyverse
和splitStackShape
的组合的一个选项。根据count
重复行后,我们可以使用row_number
减去1来为每个组创建一个序列(例如,0,1,2 ...),然后乘以10获取10年增量,然后添加到该组的十年
。或者,如果您不想加载另一个软件包,我们可以使用基本R:
输出创建重复的行
Here is one option using a combination of
tidyverse
andsplitstackshape
. After duplicating rows according toCount
, we can userow_number
minus 1 to create a sequence for each group (e.g., 0, 1, 2...), then multiply by 10 to get the 10 year increment and add to theDecade
for that group.Or if you don't want to load another package, we could create the duplicated rows with base R:
Output
序列应该很快排序:
Sequences should sort this very quickly: