我對 r 比較陌生,一直在絞盡腦汁想弄清楚如何讓我的代碼更有效率。這是我一直在使用的資料的一個子集:
mydata <- structure(list(Subject = c("1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2"), Interval = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L), XDistance = c(240, 252.5, 125, 107.5, 170, 77.5,
105, 157.5, 187.5, 125, 62.5, 187.5, 15, 130, 45, 0, 80, 205,
97.5, 85, 160, 152.5, 12.5, 107.5, 157.5, 112.5, 102.5, 82.5,
55, 57.5, 217.5, 235, 142.5, 215, 127.5, 120, 115, 167.5, 182.5,
147.5, 207.5, 90, 165, 155, 222.5, 140, 175, 72.5, 112.5, 172.5
), YDistance = c(235, 190, 145, 132.5, 210, 92.5, 160, 150, 192.5,
170, 105, 162.5, 87.5, 170, 80, 12.5, 145, 182.5, 170, 87.5,
102.5, 122.5, 0, 117.5, 247.5, 195, 145, 167.5, 97.5, 75, 395,
277.5, 245, 260, 270, 237.5, 235, 275, 245, 210, 200, 92.5, 217.5,
195, 225, 247.5, 212.5, 135, 187.5, 192.5)), row.names = c(NA,
-50L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000022000ea9850>)e here
我的目標是在 5 個間隔箱中為每個主題獲取 XDistance 和 YDistance 的平均值。每個主題總共有 30 個區間,我會為每個主題設定 6 組均值。然后我想創建一個這些方法的新表。我能夠做到這一點,但是,我以一種復雜的方式做到了。這是我使用的代碼:
mydata <- read.csv("DSR_Practice.csv")
par(mfrow = c(2,2))
library(dplyr)
mydata <- mydata[!(mydata$Subject == "Not Used"), ]
library(data.table)
setDT(mydata)
cuts <- list(c(1,5), c(6,10), c(11,15), c(16, 20), c(21, 25), c(26, 30))
data <- lapply(X = cuts, function(i) {
mydata[between(x = mydata[ , Interval], lower = i[1], upper = i[2])]
})
Bin1 <- as.data.frame(data[[1]])
Bin1 <- Bin1 %>%
group_by(Subject) %>%
summarise(across(XDistance:YDistance, mean, X = "{XDistance}.{mean}", Y = "{YDistance}. {mean}")) %>%
as.data.frame()
Bin2 <- as.data.frame(data[[2]])
Bin2 <- Bin2 %>%
group_by(Subject) %>%
summarise(across(XDistance:YDistance, mean, X = "{XDistance}.{mean}", Y = "{YDistance}. {mean}")) %>%
as.data.frame()
Bin3 <- as.data.frame(data[[3]])
Bin3 <- Bin3 %>%
group_by(Subject) %>%
summarise(across(XDistance:YDistance, mean, X = "{XDistance}.{mean}", Y = "{YDistance}. {mean}")) %>%
as.data.frame()
Bin4 <- as.data.frame(data[[4]])
Bin4 <- Bin4 %>%
group_by(Subject) %>%
summarise(across(XDistance:YDistance, mean, X = "{XDistance}.{mean}", Y = "{YDistance}. {mean}")) %>%
as.data.frame()
Bin5 <- as.data.frame(data[[5]])
Bin5 <- Bin5 %>%
group_by(Subject) %>%
summarise(across(XDistance:YDistance, mean, X = "{XDistance}.{mean}", Y = "{YDistance}. {mean}")) %>%
as.data.frame()
Bin6 <- as.data.frame(data[[6]])
Bin6 <- Bin6 %>%
group_by(Subject) %>%
summarise(across(XDistance:YDistance, mean, X = "{XDistance}.{mean}", Y = "{YDistance}. {mean}")) %>%
as.data.frame()
df.list <- list(Bin1, Bin2, Bin3, Bin4, Bin5, Bin6)
df.list <- Reduce(function(x, y) merge(x, y, all = TRUE), df.list)
任何幫助將不勝感激!
我試圖使用 for 回圈創建這些垃圾箱,還嘗試使用 lapply 來獲取所有垃圾箱串列的方法,但我似乎無法弄清楚。
uj5u.com熱心網友回復:
您可以創建一個bin
使用該函式呼叫的變數cut
,并使用以下方法進行分組計算:
mydata %>%
mutate(bin = as.numeric(cut(Interval, seq(0, 30, 5) 0.5))) %>%
group_by(Subject, bin) %>%
summarise_all(mean) %>%
select(-Interval)
#> # A tibble: 10 x 4
#> # Groups: Subject [2]
#> Subject bin XDistance YDistance
#> <chr> <dbl> <dbl> <dbl>
#> 1 1 1 179 182.
#> 2 1 2 130. 153
#> 3 1 3 88 121
#> 4 1 4 93.5 120.
#> 5 1 5 118 118
#> 6 1 6 82 136
#> 7 2 1 188. 290.
#> 8 2 2 146. 240.
#> 9 2 3 168 186
#> 10 2 4 134. 195
使用reprex v2.0.2創建于 2022-10-26
uj5u.com熱心網友回復:
我認為這種 dplyr 方法會產生您正在尋找的東西,盡管可能有一種比我使用的 case_when 系列更簡潔的方法來創建您的垃圾箱。
library(dplyr)
mydata %>%
group_by(Subject) %>%
mutate(Bin = case_when(
Interval >= 1 & Interval <= 5 ~ 1,
Interval >=6 & Interval <=10 ~ 2,
Interval >= 11 & Interval <=15 ~ 3,
Interval >=16 & Interval <=20 ~ 4,
Interval >=21 & Interval <=25 ~ 5,
Interval >=26 & Interval <=30 ~ 6,
TRUE ~ 0
)) %>%
group_by(Subject, Bin) %>%
summarize(XAvg = mean(XDistance),
YAvg = mean(YDistance))
uj5u.com熱心網友回復:
這是一種不同的方式。它不如其他人建議的那么好,但它確實有效。(它假設主題始終是一個整數,并且粘貼時有一個唯一的主題-間隔組合)它以正確的順序給出了所描述的預期輸出和間隔。
ave(mydata$Interval, mydata$Subject, FUN = function(x) ceiling((x/5))) %>%
paste0(mydata$Subject,.) %>% {tapply(mydata$XDistance,., mean)} -> XDistance
ave(mydata$Interval, mydata$Subject, FUN = function(x) ceiling((x/5))) %>%
paste0(mydata$Subject,.) %>% {tapply(mydata$YDistance,., mean)} -> YDistance
ave(mydata$Interval, mydata$Subject, FUN = function(x) ceiling((x/5))) %>%
paste0(mydata$Subject,.) %>% unique() %>% substr(.,1,1) -> Subject
df.list2 <- data.frame(Subject,XDistance,YDistance)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520903.html
標籤:r
上一篇:在變數中組合單元格值?