在 R 中从长到宽去除 NA
long to wide in R removing NA's
我有一个示例数据,看起来像
输出:
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))
我想将其转换为宽格式,使其看起来像:
我试过了
library(tidyr)
final_data <- spread(sample, key = variable, value = value)
但是我得到的输出不是所需的格式,我得到的是这种格式的输出:
我需要帮助如何摆脱 NA'S 并以所需格式重组输出。
我们需要创建一个序列变量
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
您可以执行以下操作:
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
我有一个示例数据,看起来像
输出:
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))
我想将其转换为宽格式,使其看起来像:
我试过了
library(tidyr)
final_data <- spread(sample, key = variable, value = value)
但是我得到的输出不是所需的格式,我得到的是这种格式的输出:
我需要帮助如何摆脱 NA'S 并以所需格式重组输出。
我们需要创建一个序列变量
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
您可以执行以下操作:
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