从data.frame拆分值,并创建其他行来存储其组件 [英] Split value from a data.frame and create additional row to store its component

查看:113
本文介绍了从data.frame拆分值,并创建其他行来存储其组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我有一个名为 df 的数据框,如下所示:

In R, I have a data frame called df such as the following:

A     B      C      D

a1     b1     c1     2.5

a2     b2     c2     3.5


a3     b3     c3     5 - 7

a4     b4     c4     2.5

A      B      C      D
a1    b1    c1     2.5
a2    b2    c2     3.5
a3    b3    c3     5 - 7
a4    b4    c4     2.5



我想用破折号分割第三行的值和 D 列,并创建另一行第二个值保留该行的其他值。


I want to split the value of the third row and D column by the dash and create another row for the second value retaining the other values for that row.

所以我想要这样:

A      B      C      D

a1     b1     c1     2.5

a2     b2     c2     3.5


a3     b3     c3     5


a3     b3     c3     7

a4     b4     c4     2.5

A      B      C      D
a1    b1    c1     2.5
a2    b2    c2     3.5
a3    b3    c3     5
a3    b3    c3     7
a4    b4    c4     2.5

任何想法如何实现?

理想情况下,我还要创建一个额外的列指定I split值是最小值还是最大值。

Ideally, I would also want to create an extra column to specify whether the value I split is either a minimum or maximum.

所以这样:

A      B      C      D      E

a1     b1     c1     2.5

a2     b2     c2     3.5


a3     b3     c3     5    最低


a3     b3     c3     7    最高


a4     b4     c4    

A      B      C      D      E
a1    b1    c1     2.5
a2    b2    c2     3.5
a3    b3    c3     5      min
a3    b3    c3     7      max
a4    b4    c4     2.5

谢谢。

推荐答案

一个选项是使用 sub 在D列中粘贴最小和最大值,其中找到 - ,然后使用 cSplit 分割'D'列。

One option would be to use sub to paste 'min' and 'max in the 'D" column where - is found, and then use cSplit to split the 'D' column.

library(splitstackshape)
df1$D <- sub('(\\d+) - (\\d+)', '\\1,min - \\2,max', df1$D)
res <- cSplit(cSplit(df1, 'D', ' - ', 'long'), 'D', ',')[is.na(D_2), D_2 := '']
setnames(res, 4:5, LETTERS[4:5])
res
#   A  B  C   D   E
#1: a1 b1 c1 2.5    
#2: a2 b2 c2 3.5    
#3: a3 b3 c3 5.0 min
#4: a3 b3 c3 7.0 max
#5: a4 b4 c4 2.5    

这篇关于从data.frame拆分值,并创建其他行来存储其组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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