R:基于其他列填充和/或复制行 [英] R: populating and/or duplicating rows based upon other columns
问题描述
我的问题是基于这个问题。
我有一个数据如下。我想先填充细胞,首先往下看,然后通过查找,只要bom是相同的。在bom = A的情况下,我想填充行,如图所示。但是在bom = B的情况下,由于type_p列不同,我想重复行并感受空白
I have a data as below. I want to fill cells by first looking down and then by looking up as long as the bom is same. In case of bom=A, I want to fill up rows as shown. But in case of bom=B, as the type_p column is different, I want to duplicate rows and feel the blanks
bom=c(rep("A",4),rep("B",3))
Part=c("","lambda","beta","","tim","tom","")
type_p=c("","sub","sub","","sub","pan","")
ww=c(1,2,3,4,1,2,3)
df=data.frame(bom,Part,type_p,ww)
> df
bom Part type_p ww
1 A 1
2 A lambda sub 2
3 A beta sub 3
4 A 4
5 B tim sub 1
6 B tom pan 2
7 B 3
我的最终数据想要如下
bom Part type_p ww
1 A lambda sub 1
2 A lambda sub 2
3 A beta sub 3
4 A beta sub 4
5 B tim sub 1
6 B tim sub 2
7 B tim sub 3
5 B tom pan 1
6 B tom pan 2
7 B tom pan 3
________________________________________更新1
我想要的逻辑如下。请记住,我的数据非常庞大,每列中有数千个数值。
________________________________________Update 1
The logic that I want is as below. Please remember that my data is very huge and I have thousands of values in each column.
bom和ww列总是填入/填入传入数据
bom and ww columns are always populated/filled in incoming data
- 检查如果列bom中的一个条目在列type_p
- 中的值超过1个值,那么只有1个值,然后首先向下看,然后查找,才能填充type_p和ww列中的空白。在这种情况下,bom = A在type_p(sub)中只有一个值
- 如果列bom中的一个条目在type_p列中具有多于1个唯一值,则创建另外一组相同的行这个bom使得总集合将等于该bom的type_p列中的不同值。在这种情况下,bom = B在type_p(sub和pan)中有两个值。
- 首先向下看,然后查找(查看源行以填充),在type_p和ww列中填充空白价值)
- Check if an entry in column bom has more than 1 value in the column type_p
- If there is only 1 value then fill blanks in type_p and ww columns by first looking down and then looking up. In this case bom=A has only one value in type_p (sub)
- If an entry in column bom has more than 1 unique value in type_p column then create additional sets of the same rows of that bom such that total sets will be equal to distinct values in type_p column for that bom. In this case bom=B has two values in type_p (sub and pan)
- Fill blanks in type_p and ww columns by first looking down and then looking up (look at the source row to fill up values)
===================更新2
在步骤3之后,数据框将如下所示
After step 3, the data frame would look like below
> df
bom Part type_p ww
1 A lambda sub 1
2 A lambda sub 2
3 A beta sub 3
4 A beta sub 4
5 B tim sub 1
6 B 2
7 B 3
8 B 1
9 B tom pan 2
10 B 3
推荐答案
使用 tidyr
和 dplyr
,你可以设法在你的目标附近做一些事情。
With tidyr
and dplyr
, you could manage to do something near what you aim
library(tidyr)
library(dplyr)
# replacing empty string with NA
df <- df %>% mutate_each(funs(sub("^$", NA, .)), Part, type_p)
# filling missing values
df <- df %>% fill(Part, type_p,.direction = "down") %>% fill(Part, type_p,.direction = "up")
df
#> bom Part type_p ww
#> 1 A lambda sub 1
#> 2 A lambda sub 2
#> 3 A beta sub 3
#> 4 A beta sub 4
#> 5 B tim sub 1
#> 6 B tom pan 2
#> 7 B tom pan 3
要获得您所描述的内容(有问题和评论),您可以对待BOM A& B分开:
To obtain what you described (in question and comment), you could treat BOM A & B separately:
bind_rows(
df %>% filter(bom == "A"),
df %>% filter(bom == "B") %>%
complete(nesting(bom, Part, type_p), ww)
)
#> Source: local data frame [10 x 4]
#>
#> bom Part type_p ww
#> (fctr) (chr) (chr) (dbl)
#> 1 A lambda sub 1
#> 2 A lambda sub 2
#> 3 A beta sub 3
#> 4 A beta sub 4
#> 5 B tim sub 1
#> 6 B tim sub 2
#> 7 B tim sub 3
#> 8 B tom pan 1
#> 9 B tom pan 2
#> 10 B tom pan 3
这篇关于R:基于其他列填充和/或复制行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!