根据另一列中的相应值替换一列中的空单元格?

时间:2017-03-31 14:11:26

标签: r dataframe dplyr

我有这个数据框:

/*In PDO*/
$pid = "test123";
$pdo = new PDO("mysql:host=localhost;dbname=database", 'user', 'password');
/*Putting backtick around columns that are reserved words*/
$query = "SELECT `name` FROM my_table WHERE
            `channel`= :pid";
$pdo_stmt = $pdo->prepare($query);
/*Prepared statements escape it for you*/
$pdo_stmt->bindValue(":pid", $pid);
$pdo_stmt->execute();

echo $pdo_stmt->fetchColumn();

看起来像这样:

df <- data.frame(number = c(1,2,3,1,3,4,5,2), 
             name = c('dany', 'rand', 'tanya', 
                      ' ', ' ', 'bruce', 'tony', ' '), 
             grade = c('a', 'b', 'c', ' ', 'c', 'd', 'e', ' '))

我想要的是:

 > df
# A tibble: 8 × 3
number  name grade
 <dbl> <chr> <chr>
    1  dany     a
    2  rand     b
    3 tanya     c 
    1         
    3           c
    4 bruce     d
    5  tony     e
    2                    

我如何做到这一点? 如何使用与数字列对应的值替换空格?

如果可能,请说明如何使用 > df # A tibble: 8 × 3 number name grade <dbl> <chr> <chr> 1 dany a 2 rand b 3 tanya c 1 dany a 3 tanya c 4 bruce d 5 tony e 2 rand b 实现此目的。

3 个答案:

答案 0 :(得分:2)

如果你有NA,那将会更容易处理

library(dplyr)
library(tidyr)

df[df == ' '] <- NA

df %>% 
 group_by(number) %>% 
 fill(name, grade)

#Source: local data frame [8 x 3]
#Groups: number [5]

#  number  name grade
#   <dbl> <chr> <chr>
#1      1  dany     a
#2      1  dany     a
#3      2  rand     b
#4      2  rand     b
#5      3 tanya     c
#6      3 tanya     c
#7      4 bruce     d
#8      5  tony     e

答案 1 :(得分:1)

这是我的解决方案,假设只有数字在某处有现有的完整行:

library(tidyverse)

df <- data_frame(number = c(1,2,3,1,3,4,5,2), 
                 name = c('dany', 'rand', 'tanya', 
                          ' ', ' ', 'bruce', 'tony', ' '), 
                 grade = c('a', 'b', 'c', ' ', 'c', 'd', 'e', ' '))

master <- df %>%
  filter(name != " " & grade != "")
master <- master[!duplicated(master),]

only_number <- df %>%
  select(number)

df <- inner_join(only_number, master, by = c("number"))

答案 2 :(得分:1)

我会使用merge

在基地R中执行此操作
x <- df$name==" "
A <- df[x,]
B <- df[!x,]
rbind(B, setNames(merge(A, B, by = "number")[,c(1,4,5)], colnames(a)))

   # number  name grade
# 1       1  dany     a
# 2       2  rand     b
# 3       3 tanya     c
# 6       4 bruce     d
# 7       5  tony     e
# 11      1  dany     a
# 21      2  rand     b
# 31      3 tanya     c