总结原始数据透视表r中不同长度向量的结果
我想使用向量:总结原始数据透视表r中不同长度向量的结果,r,vector,sum,pivot-table,R,Vector,Sum,Pivot Table,我想使用向量: time.int<-c(1,2,3,4,5) #vector to be use as a "guide" time.int以下内容将生成向量freq freq <- sapply(time.int, function(x) sum(x == time)) freq [1] 4 0 0 0 3 为了将上面的代码推广到任意长度的time.int段,我相信下面的函数可以做到这一点。请注意,由于更改了数据,n==1的输出与上面的不同 fun <- functi
time.int<-c(1,2,3,4,5) #vector to be use as a "guide"
time.int以下内容将生成向量freq
freq <- sapply(time.int, function(x) sum(x == time))
freq
[1] 4 0 0 0 3
为了将上面的代码推广到任意长度的time.int
段,我相信下面的函数可以做到这一点。请注意,由于更改了数据,n==1
的输出与上面的不同
fun <- function(x, y, n){
inx <- lapply(seq_len(length(x) %/% n), function(m) seq_len(n) + n*(m - 1))
sapply(inx, function(i) sum(y %in% x[i]))
}
freq1 <- fun(time.int, time, 1)
freq1
[1] 3 1 0 0 3 1
freq2 <- fun(time.int, time, 2)
freq2
[1] 4 0 4
freq3 <- fun(time.int, time, 3)
freq3
[1] 4 4
fun以下内容将生成向量freq
freq <- sapply(time.int, function(x) sum(x == time))
freq
[1] 4 0 0 0 3
为了将上面的代码推广到任意长度的time.int
段,我相信下面的函数可以做到这一点。请注意,由于更改了数据,n==1
的输出与上面的不同
fun <- function(x, y, n){
inx <- lapply(seq_len(length(x) %/% n), function(m) seq_len(n) + n*(m - 1))
sapply(inx, function(i) sum(y %in% x[i]))
}
freq1 <- fun(time.int, time, 1)
freq1
[1] 3 1 0 0 3 1
freq2 <- fun(time.int, time, 2)
freq2
[1] 4 0 4
freq3 <- fun(time.int, time, 3)
freq3
[1] 4 4
fun我们可以使用table
函数来计算事件数,并使用merge
创建一个数据框来汇总信息<代码>事件数据是最终输出
# Create example data
time.int <- c(1,2,3,4,5)
time <- c(1,1,1,1,5,5,5)
# Count the event using table and convert to a data frame
event <- as.data.frame(table(time))
# Convert the time.int to a data frame
time_dat <- data.frame(time = time.int)
# Merge the data
event_dat <- merge(time_dat, event, by = "time", all = TRUE)
# Replace NA with 0
event_dat[is.na(event_dat)] <- 0
# See the result
event_dat
time Freq
1 1 4
2 2 0
3 3 0
4 4 0
5 5 3
#创建示例数据
time.int我们可以使用table
函数来计算事件数,并使用merge
创建一个数据框来汇总信息<代码>事件数据是最终输出
# Create example data
time.int <- c(1,2,3,4,5)
time <- c(1,1,1,1,5,5,5)
# Count the event using table and convert to a data frame
event <- as.data.frame(table(time))
# Convert the time.int to a data frame
time_dat <- data.frame(time = time.int)
# Merge the data
event_dat <- merge(time_dat, event, by = "time", all = TRUE)
# Replace NA with 0
event_dat[is.na(event_dat)] <- 0
# See the result
event_dat
time Freq
1 1 4
2 2 0
3 3 0
4 4 0
5 5 3
#创建示例数据
时间,太棒了!如果我想概括一下?为了能够在新向量中以“时间”表示每3个时间数的事件总和。int而不是1乘1?@havefun也许你可以使用嵌套的sapply
,但你需要编辑你的问题并说出预期的输出。我现在编辑了我的问题,理想情况下,我想在开始时选择一个参数,并用它来划分向量。太棒了!如果我想概括一下?为了能够在新向量中以“时间”表示每3个时间数的事件总和。int而不是1乘1?@havefun也许你可以使用嵌套的sapply
,但你需要编辑你的问题并说出预期的输出。我现在编辑了我的问题,理想情况下,我希望在开始时选择一个参数,并使用它来分割向量。
dat1 <- data.frame(time,value))
fun <- function(x, y, n){
inx <- lapply(seq_len(length(x) %/% n), function(m) seq_len(n) + n*(m - 1))
sapply(inx, function(i) sum(y %in% x[i]))
}
freq1 <- fun(time.int, time, 1)
freq1
[1] 3 1 0 0 3 1
freq2 <- fun(time.int, time, 2)
freq2
[1] 4 0 4
freq3 <- fun(time.int, time, 3)
freq3
[1] 4 4
# Create example data
time.int <- c(1,2,3,4,5)
time <- c(1,1,1,1,5,5,5)
# Count the event using table and convert to a data frame
event <- as.data.frame(table(time))
# Convert the time.int to a data frame
time_dat <- data.frame(time = time.int)
# Merge the data
event_dat <- merge(time_dat, event, by = "time", all = TRUE)
# Replace NA with 0
event_dat[is.na(event_dat)] <- 0
# See the result
event_dat
time Freq
1 1 4
2 2 0
3 3 0
4 4 0
5 5 3