我试图改变从BA,SLG到SLG,BA的方面的顺序.我发现了与此类似的问题,但我认为我的解决方案可能无法正常工作,因为我已经在excel中汇总了数据; 因此,我的数据框可能不同.无论如何,我试图实现这个无济于事:
df2 <- factor(df, levels = c("SLG","BA"))
Run Code Online (Sandbox Code Playgroud)
任何帮助解决这个问题将非常感激.
df <- read.table(textConnection(
'POS SLG BA
2B 0.4632 .23
3B 0.468652174 .24
SS 0.4146 .22
1B 0.472368421 .25
RF 0.462684211 .245
CF 0.4435 .225
LF 0.4474 .226
C 0.440875 .228
DH 0.508714286 .28'), header = TRUE,stringsAsFactors = FALSE)
library(micromapST)
library(ggplot2)
library(tidyr)
library(dplyr)
df$POS <- reorder(as.factor(df$POS), df$SLG)
dfx <- gather(df, group, data, SLG, BA)
row.names(df) <- NULL
theme_set(theme_grey() +
theme(plot.title = element_text(hjust=0.5,face='bold'),
axis.title.y = element_text(angle = 0, vjust = 0.5,face='bold'),
axis.title.x=element_text(face='bold'),
panel.background …Run Code Online (Sandbox Code Playgroud) 创建随着组变量中的每次更改而递增的数字序列的有效方法是什么?作为一个玩具示例,使用下面的数据框,我想要一个新变量“Value”来接受值c(1,1,1,2,2,3,3,4)。请注意,即使 48 重复,“值”仍然会增加,因为我只关心序列的变化。
df <- read.table(textConnection(
'Group
48
48
48
56
56
48
48
14'), header = TRUE)
Run Code Online (Sandbox Code Playgroud)
一种方法是
df$Value<-1
for(i in 2:nrow(df)){
if(df[i,]$Group==df[i-1,]$Group){df[i,]$Value=df[i-1,]$Value}
else{df[i,]$Value=df[i-1,]$Value+1}
}
Run Code Online (Sandbox Code Playgroud)
但这非常慢。我的实际数据集有几百万个观察值。
注意:我在措辞这个问题的标题时遇到了困难,所以如果您愿意,请更改它。
我试图在R中获得Bonferroni同时置信区间.我有以下数据集,我为实践做了补充:
df2 <- read.table(textConnection(
'group value
1 25
2 36
3 42
4 50
1 27
2 35
3 49
4 57
1 22
2 37
3 45
4 51'), header = TRUE)
Run Code Online (Sandbox Code Playgroud)
我试过了
aov(formula = value ~ group, data = df2)
Run Code Online (Sandbox Code Playgroud)
但是,这不会同时输出置信区间.使用SAS,计算结果如下: