在列中输入字符值

时间:2017-03-14 10:01:36

标签: r imputation

我的代码如下所示:

DECLARE @Table TABLE (Rowid int,batch varchar(100),maker varchar(100),checker varchar(100),date varchar(100))
DECLARE @Table1 TABLE (batch varchar(100),maker varchar(100),checker varchar(100))
INSERT INTO  @Table
SELECT 1,101,'john',null,'4feb' UNION ALL
SELECT 2,101,'john','raja','4feb' UNION ALL
SELECT 3,101,'john',null,'5feb' UNION ALL
SELECT 4,101,'john','raja','6feb' UNION ALL
SELECT 5,101,'john',null,'9feb'

DECLARE @i INT=1,@RowId INT
WHILE @i<=(SELECT COUNT(*) FROM @Table)
BEGIN
SELECT @RowId= CASE WHEN ISNULL(checker,'0')='0' THEN @i+1 ELSE @i END FROM @Table WHERE Rowid=@i
INSERT INTO @Table1 SELECT batch,maker,checker FROM @Table WHERE Rowid=@RowId
IF @@ROWCOUNT=0
INSERT INTO @Table1 SELECT batch,maker,checker FROM @Table WHERE Rowid=@RowId-1
SET @i=@i+2
END
SELECT * FROM @Table1

我想要输入值并使用与每个“Item”对应的值填充“Category”列,只要它不是空白。最终结果应该是这样的:

Item | Category
A    |    1
A    |
A    |
A    |    1
A    |
A    |
A    |    1
B    |    2
B    |
B    |
B    |    2
B    |
B    |
B    |    2
B    |
B    |

我怎样才能在R?

中这样做

1 个答案:

答案 0 :(得分:1)

我们可以使用fill

中的tidyverse
library(tidyverse)
df1 %>% 
    fill(Category)