我正在尝试使用以下代码将长格式转换为宽格式。
data_ige<-read.csv("serology.csv",header = TRUE,na.strings=0)
library(tidyverse) library(magrittr)
data_new <- data_ige %>% spread(test, value)
我有以下数据集 existing dataset
运行代码后,它转换日期(但不是我想要的方式),如下图所示,以黄色突出显示的项目表明这些值位于多行中,但应位于第一行而不是新的一行。每位患者都有1次或2次就诊的数据。因此,所有来自1次访问的测试结果,我希望在一行中看到它们,而在第二行中访问2的测试结果。
此屏幕快照显示了所需的结果。
答案 0 :(得分:1)
由于重复项,我们需要创建一个序列列
library(dplyr)
library(tidyr)
data_ige %>%
group_by(ID, date, test) %>%
mutate(rn = row_number()) %>%
ungroup %>%
spread(test, value) %>%
#or use pivot_wider as spread is getting deprecated
# pivot_wider(names_from = test, values_from = value) %>%
select(-rn)
# A tibble: 8 x 9
# ID date `1` `3` `4` `5` `6` `7` `8`
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 A 2008 0.035 NA NA NA NA NA NA
#2 A 2011 2.75 NA NA NA NA NA NA
#3 B 2011 9.99 3.65 0.68 0.02 0.17 0.5 NA
#4 C 2008 0 NA NA NA NA NA NA
#5 C 2011 NA NA NA NA NA NA 0.09
#6 D 2008 0 0 0 0 0 0.59 0
#7 D 2011 0 0.49 0.2 0.08 0.16 0.5 0.13
#8 D 2011 9.99 NA NA NA NA NA NA
data_ige <- structure(list(ID = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("A", "B", "C", "D"), class = "factor"), date = c(2008,
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2008, 2011, 2008,
2008, 2008, 2008, 2008, 2008, 2008, 2011, 2011, 2011, 2011, 2011,
2011, 2011), test = c(1, 1, 1, 3, 4, 5, 6, 7, 8, 1, 1, 1, 3,
4, 5, 6, 7, 8, 1, 3, 4, 5, 6, 7, 8), value = c(0.035, 2.75, 9.99,
3.65, 0.68, 0.02, 0.17, 0.5, 0.09, 0, 0, 0, 0, 0, 0, 0, 0.59,
0, 9.99, 0.49, 0.2, 0.08, 0.16, 0.5, 0.13)),
class = "data.frame", row.names = c(NA,
-25L))
答案 1 :(得分:1)
当我使用链接到的表时,它实际上对我来说很好。您的数据可能存在问题,即您可能已将字符串作为因素导入。尝试使用下面提供的数据:
data_ige %>% spread(test, value)
#### OUTPUT ####
ID date 1 3 4 5 6 7 8
1 A 2008 0.035 NA NA NA NA NA NA
2 A 2011 2.750 NA NA NA NA NA NA
3 B 2011 9.990 3.65 0.68 0.02 0.17 0.50 0.09
4 C 2008 0.000 NA NA NA NA NA NA
5 C 2011 0.000 NA NA NA NA NA NA
6 D 2008 0.000 0.00 0.00 0.00 0.00 0.59 0.00
7 D 2011 9.990 0.49 0.20 0.08 0.16 0.50 0.13
您可能想做的一件事是为test == 2
添加一行,该行不在您的数据中。这样,您将获得仅包含2
的列NA
,就像您链接到的数据框的图像一样:
data_ige %>%
add_row(ID = "A", date = 2008, test = 2) %>%
spread(test, value)
#### OUTPUT ####
ID date 1 2 3 4 5 6 7 8
1 A 2008 0.035 NA NA NA NA NA NA NA
2 A 2011 2.750 NA NA NA NA NA NA NA
3 B 2011 9.990 NA 3.65 0.68 0.02 0.17 0.50 0.09
4 C 2008 0.000 NA NA NA NA NA NA NA
5 C 2011 0.000 NA NA NA NA NA NA NA
6 D 2008 0.000 NA 0.00 0.00 0.00 0.00 0.59 0.00
7 D 2011 9.990 NA 0.49 0.20 0.08 0.16 0.50 0.13
这是我使用的数据框:
data_ige <- structure(list(ID = c("A", "A", "B", "B", "B", "B", "B", "B",
"B", "C", "C", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D",
"D", "D", "D", "D"), date = c(2008L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2008L, 2011L, 2008L, 2008L, 2008L,
2008L, 2008L, 2008L, 2008L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L), test = c(1L, 1L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
1L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 3L, 4L, 5L, 6L, 7L, 8L),
value = c(0.035, 2.75, 9.99, 3.65, 0.68, 0.02, 0.17, 0.5,
0.09, 0, 0, 0, 0, 0, 0, 0, 0.59, 0, 9.99, 0.49, 0.2, 0.08,
0.16, 0.5, 0.13)), class = "data.frame", row.names = c(NA,
-25L))