切换数据框中的列和行,并在单独的列标题下列出观察结果以执行方差分析:单因素
Switching columns and rows in a data frame, and listing the observations under seperate column headings to perform an Anova: Single Factor
概览
我有一个名为 df1 的数据框,其中包含两列:(1) Urbanisaiton_index(包含 **四个子级别 (1-4) ;和 (2) Canopy_Index
对于数据分析,我想进行单个方差分析来区分 Urbanisation_index 子级别组内部和子级别组之间的总体方差,以了解 Canopy_Index 的差异。这个想法是为了区分不同程度的城市化是否会影响树种的冠层覆盖范围 Quercus petraea。
为了进行方差分析,我需要翻转数据框中的列并制作一个新的数据框。我希望列标题为 1、2、3、4 以表示 Urbanisation_index 的四个组 or/sublevels 中的差异。其次,我想将属于每个子级别的 Canopy_Index 值列出到它们特定的子级别列中(参见期望的结果)。
构建所需的新数据框后,数据将以正确的格式分组以进行方差分析。
我尝试了很多不同的方法,例如转置,但我不知道如何将 urbansation_index 子级别 (1-4) 列为列标题并编译它们相关的 Canopy_Index 值(即Canopy_Index 每个 Urbanisation_index 子级别的行数)在其特定列下方。
例如,如果针对 Urbanisation_index、子级别 1 过滤数据框,则 Canopy_Index 可能有 6 个观察值(5、5、5、5、55、55),我希望它们列在新数据框中列标题 1 的下方,如下所示。
如果有人能提供帮助,我将不胜感激。
Rcode
##transpose
t(df1)
想要的结果
1 2 3 4
65 55 5 35
45 85 55 45
75 75 15 25
数据
structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4,
4, 2, 4, 3, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
2, 2, 2, 4, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 4, 4, 4,
4, 4, 4, 4), Canopy_Index = c(65, 75, 55, 85, 85, 85, 95, 85,
85, 45, 65, 75, 75, 65, 35, 75, 65, 85, 65, 95, 75, 75, 75, 65,
75, 65, 75, 95, 95, 85, 85, 85, 75, 75, 65, 85, 75, 65, 55, 95,
95, 95, 95, 45, 55, 35, 55, 65, 95, 95, 45, 65, 45, 55)), row.names = c(NA,
-54L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x1030086e0>, index = structure(integer(0), "`__Species`" = integer(0)))
使用您提供的数据:
data<-structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4,
4, 2, 4, 3, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
2, 2, 2, 4, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 4, 4, 4,
4, 4, 4, 4),
Canopy_Index = c(65, 75, 55, 85, 85, 85, 95, 85,
85, 45, 65, 75, 75, 65, 35, 75, 65, 85, 65, 95, 75, 75, 75, 65,
75, 65, 75, 95, 95, 85, 85, 85, 75, 75, 65, 85, 75, 65, 55, 95,
95, 95, 95, 45, 55, 35, 55, 65, 95, 95, 45, 65, 45, 55)),
row.names = c(NA,
-54L),
class = c("data.table", "data.frame"),
index = structure(integer(0), "`__Species`" = integer(0)))
正在加载包
library(tidyr)
library(dplyr)
library(purrr)
首先按城市化指数对冠层指数的值进行分组,得到所有值的列表,并附加它们以调整长度。
a<-data %>%
group_by(Urbanisation_index) %>%
summarise(Canopy_Indexes=paste(Canopy_Index, collapse = "-")) %>%
spread(key = Urbanisation_index, value = Canopy_Indexes) %>%
map(.f = ~ separate_rows(data.frame(.), 1, sep = "-"))
a <- lapply(a, function(x){
x1<-x[,1]
length(x1) <- max(sapply(a, nrow))
x1
}) %>% data.frame()
colnames(a) <- paste("sub_level", 1:4, sep = "_")
a
这是另一个更紧凑的解决方案,但是因为我先用了上一个,所以不想浪费它:)
b <- map(split(data, data$Urbanisation_index), 2)
b <- lapply(b, function(x){
x1<-x
length(x1) <- max(sapply(b, length))
x1
}) %>% data.frame()
colnames(b) <- paste("sub_level", 1:4, sep = "_")
b
结果:
sub_level_1 sub_level_2 sub_level_3 sub_level_4
1 35 65 85 55
2 75 75 85 85
3 65 45 75 95
4 85 95 65 85
5 55 85 95 85
6 55 85 75 65
7 NA 85 75 75
8 NA 85 75 65
9 NA 75 65 75
10 NA 65 75 75
11 NA 95 65 65
12 NA 95 75 95
13 NA 95 95 95
14 NA 95 65 45
15 NA 45 NA 65
16 NA 55 NA 45
17 NA 35 NA 55
希望对您有所帮助
概览
我有一个名为 df1 的数据框,其中包含两列:(1) Urbanisaiton_index(包含 **四个子级别 (1-4) ;和 (2) Canopy_Index
对于数据分析,我想进行单个方差分析来区分 Urbanisation_index 子级别组内部和子级别组之间的总体方差,以了解 Canopy_Index 的差异。这个想法是为了区分不同程度的城市化是否会影响树种的冠层覆盖范围 Quercus petraea。
为了进行方差分析,我需要翻转数据框中的列并制作一个新的数据框。我希望列标题为 1、2、3、4 以表示 Urbanisation_index 的四个组 or/sublevels 中的差异。其次,我想将属于每个子级别的 Canopy_Index 值列出到它们特定的子级别列中(参见期望的结果)。
构建所需的新数据框后,数据将以正确的格式分组以进行方差分析。
我尝试了很多不同的方法,例如转置,但我不知道如何将 urbansation_index 子级别 (1-4) 列为列标题并编译它们相关的 Canopy_Index 值(即Canopy_Index 每个 Urbanisation_index 子级别的行数)在其特定列下方。
例如,如果针对 Urbanisation_index、子级别 1 过滤数据框,则 Canopy_Index 可能有 6 个观察值(5、5、5、5、55、55),我希望它们列在新数据框中列标题 1 的下方,如下所示。
如果有人能提供帮助,我将不胜感激。
Rcode
##transpose
t(df1)
想要的结果
1 2 3 4
65 55 5 35
45 85 55 45
75 75 15 25
数据
structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4,
4, 2, 4, 3, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
2, 2, 2, 4, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 4, 4, 4,
4, 4, 4, 4), Canopy_Index = c(65, 75, 55, 85, 85, 85, 95, 85,
85, 45, 65, 75, 75, 65, 35, 75, 65, 85, 65, 95, 75, 75, 75, 65,
75, 65, 75, 95, 95, 85, 85, 85, 75, 75, 65, 85, 75, 65, 55, 95,
95, 95, 95, 45, 55, 35, 55, 65, 95, 95, 45, 65, 45, 55)), row.names = c(NA,
-54L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x1030086e0>, index = structure(integer(0), "`__Species`" = integer(0)))
使用您提供的数据:
data<-structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4,
4, 2, 4, 3, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
2, 2, 2, 4, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 4, 4, 4,
4, 4, 4, 4),
Canopy_Index = c(65, 75, 55, 85, 85, 85, 95, 85,
85, 45, 65, 75, 75, 65, 35, 75, 65, 85, 65, 95, 75, 75, 75, 65,
75, 65, 75, 95, 95, 85, 85, 85, 75, 75, 65, 85, 75, 65, 55, 95,
95, 95, 95, 45, 55, 35, 55, 65, 95, 95, 45, 65, 45, 55)),
row.names = c(NA,
-54L),
class = c("data.table", "data.frame"),
index = structure(integer(0), "`__Species`" = integer(0)))
正在加载包
library(tidyr)
library(dplyr)
library(purrr)
首先按城市化指数对冠层指数的值进行分组,得到所有值的列表,并附加它们以调整长度。
a<-data %>%
group_by(Urbanisation_index) %>%
summarise(Canopy_Indexes=paste(Canopy_Index, collapse = "-")) %>%
spread(key = Urbanisation_index, value = Canopy_Indexes) %>%
map(.f = ~ separate_rows(data.frame(.), 1, sep = "-"))
a <- lapply(a, function(x){
x1<-x[,1]
length(x1) <- max(sapply(a, nrow))
x1
}) %>% data.frame()
colnames(a) <- paste("sub_level", 1:4, sep = "_")
a
这是另一个更紧凑的解决方案,但是因为我先用了上一个,所以不想浪费它:)
b <- map(split(data, data$Urbanisation_index), 2)
b <- lapply(b, function(x){
x1<-x
length(x1) <- max(sapply(b, length))
x1
}) %>% data.frame()
colnames(b) <- paste("sub_level", 1:4, sep = "_")
b
结果:
sub_level_1 sub_level_2 sub_level_3 sub_level_4
1 35 65 85 55
2 75 75 85 85
3 65 45 75 95
4 85 95 65 85
5 55 85 95 85
6 55 85 75 65
7 NA 85 75 75
8 NA 85 75 65
9 NA 75 65 75
10 NA 65 75 75
11 NA 95 65 65
12 NA 95 75 95
13 NA 95 95 95
14 NA 95 65 45
15 NA 45 NA 65
16 NA 55 NA 45
17 NA 35 NA 55
希望对您有所帮助