在R中移除NA'

时间:2016-03-13 12:51:53

标签: r reshape2 tidyr

我有一个类似

的示例数据

Dput:

structure(list(variable = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), .Label = c("firstname", "lastname", "title"), class = "factor"), 
    value = structure(c(6L, 2L, 5L, 1L, 3L, 5L, 7L, 8L, 4L), .Label = c("adam", 
    "dingler", "jhon", "miss", "mr", "naji", "stephanie", "williams"
    ), class = "factor")), .Names = c("variable", "value"), class = "data.frame", row.names = c(NA, 
-9L))

我想将其转换为宽格式,使其看起来像:

enter image description here

我试过

library(tidyr) final_data <- spread(sample, key = variable, value = value) 但我得到的输出不是所需的格式,我得到这种格式的输出:

enter image description here

我需要帮助如何摆脱NA&#S;并以所需格式重组输出。

2 个答案:

答案 0 :(得分:2)

我们需要创建一个序列变量

library(dplyr)
library(tidyr)
sample %>%
     group_by(variable) %>% 
     mutate(n = row_number()) %>%
     spread(variable, value) %>%
     select(-n)
#    firstname lastname  title
#      (fctr)   (fctr) (fctr)
#1      naji  dingler     mr
#2      adam     jhon     mr
#3 stephanie williams   miss

答案 1 :(得分:0)

您可以执行以下操作:

data <- structure(list(variable = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 
                                        1L, 2L, 3L), .Label = c("firstname", "lastname", "title"), class = "factor"), 
                 value = structure(c(6L, 2L, 5L, 1L, 3L, 5L, 7L, 8L, 4L), .Label = c("adam", 
                                                                                     "dingler", "jhon", "miss", "mr", "naji", "stephanie", "williams"
                 ), class = "factor")), .Names = c("variable", "value"), class = "data.frame", row.names = c(NA, 


firstname <- data$value[which(data$variable == "firstname")]
lastname <- data$value[which(data$variable == "lastname")]
title <- data$value[which(data$variable == "title")]

data_new <- data.frame(firstname, lastname, title)
data_new