从csv中的单元格引用变量计算平均值

时间:2019-07-31 01:58:56

标签: r mean calculation

我是R的新手,正在旅途中学习,因此请原谅我的一些无知。我试图写我想要的东西,但是一路上遇到了一些障碍...

我从同事那里改编了这段代码,以确定力时跟踪的不同阶段。代码如下。它读取csv,然后使用带有新csv的帧引用导出。

根据特定规则定义了跟踪的每个阶段。

但是,我想知道开始推进和结束推进之间的平均总力,平均速度和平均功率?开始和结束推进的输出是单元格参考,而不是数据值。

第二,如何在代码中编写循环,以便我可以指导代码从一个文件夹读取40个csv文件,并生成具有所需输出的40个csv文件?

文件夹= Common2 \ Desktop \ R \ Jump_Files

先谢谢了。

D

我在编写均值公式以生成值时遇到麻烦。我知道会是

  

mean.force =平均值(start.propulsion:end.propulsion)

但这似乎不起作用?我知道这与使用值而不是单元格引用有关,但是我在这一点上被困住了

代码下方的问题...

# load required packages
 require(readr)
 library(tidyverse)
getwd()

# import data frame
filename <- "P01_CMJ_T01.csv"
datapath <- ''
Jump.1 = read_csv(
  paste0(datapath, filename),
  col_names = TRUE,
  skip = 3,
  trim_ws = TRUE
)


# add rownumber
Jump.1$row = 1:nrow(Jump.1)

# rename variable, sub frame to time
names(Jump.1) <- c('Frame','Time','F1','F2','sum.force','row.number')



# find sample rate
Sample.rate = 1 / diff(Jump.1$Time[1:2], lag = 1, differences = 1)

# find body mass
BM = mean(Jump.1$sum.force[1:Sample.rate])
BM.kg = BM / 9.81
sd = sd(Jump.1$sum.force[1:Sample.rate])

# add variables
Jump.1 = mutate(Jump.1, net.force    = Jump.1$sum.force - BM)
Jump.1 = mutate(Jump.1, acceleration = Jump.1$net.force / BM.kg)

# velocity
Jump.1$change_vel = (Jump.1$acceleration + lag(Jump.1$acceleration, 1)) / 2 * c(NA, diff(Jump.1$Time))
Jump.1$velocity = c(0, rep(NA, nrow(Jump.1) - 1))
for (i in 2:nrow(Jump.1)) {
  Jump.1$velocity[i] <- Jump.1$velocity[i - 1] + Jump.1$change_vel[i]
}

#power
Jump.1 = mutate(Jump.1, power = Jump.1$velocity * Jump.1$sum.force)

# ******************************************************************************
# Start of unweighting phase - BM - 5sd 
## [only flag if remain below threshold for samplerate/10 rows]
threshold = BM - 5 * sd

# Get all the rows less than threshold
start.unweighting = which(Jump.1$sum.force < threshold)
#show(start.unweighting)

# Finds all the sub-sequences
subseq <- split(start.unweighting, cumsum(c(TRUE, diff(start.unweighting) != 1)))
#show(subseq)

# Throw away the sub-sequences less than samplerate/10 length
subseq <- subseq[sapply(subseq, length) >= Sample.rate / 10]
#show(subseq)

# The start of unweighing is then the first row of the first sub-sequence
start.unweighting <- subseq[[1]][1]
show(start.unweighting)

# ******************************************************************************
# End of unweighting phase

# All rows > theshold and AFTER start-unweigh
end.unweighting = which(Jump.1$sum.force > threshold & Jump.1$row.number > start.unweighting)
#show(end.unweighting)

# Finds all the sub-sequences
subseq <- split(end.unweighting, cumsum(c(TRUE, diff(end.unweighting) != 1)))
#show(subseq)

# Throw away the sub-sequences less than samplerate/10 length
subseq <- subseq[sapply(subseq, length) >= Sample.rate / 10]
#show(subseq)

end.unweighting <- subseq[[1]][1] - 1
show(end.unweighting)

# ******************************************************************************
# Start braking
start.braking = end.unweighting + 1
show(start.braking)

# ******************************************************************************
# End braking
# All rows with vel > 0 and AFTER start braking
end.braking = which(Jump.1$velocity > 0 &
                      Jump.1$row.number > start.braking)[1] - 1
show(end.braking)

# ******************************************************************************
# Start propulsion phase - vel > 0 > e
start.propulsion = end.braking + 1
show(start.propulsion)

# ******************************************************************************
# End propulsion
end.propulsion = which(Jump.1$sum.force < threshold &
                         Jump.1$row.number > start.propulsion)[1] - 1
show(end.propulsion)

# ******************************************************************************
# Start flight
start.flight = end.propulsion + 1
mid.flight = which(Jump.1$sum.force == 0 &
                     Jump.1$row.number > start.flight)[1]
# ******************************************************************************
# End flight
end.flight = which(Jump.1$sum.force > 10 -1 &
                     Jump.1$row.number > mid.flight)[1]

# ******************************************************************************
# Flight.time
Flight.time = (end.flight - start.flight) * 0.001
show(Flight.time)

# ******************************************************************************
# Jump.height from flight time
Jump.height = (0.125 * 9.8 * Flight.time^2)
show(Jump.height)

 ******************************************************************************
# Write output
output <- data.frame(
  event = c(
    'unweighting.start',
    'unweighting.end',
    'braking-start',
    'braking-end',
    'propulsion.start',
    'propulsion.end',
    'flight.start',
    'flight.end',
    'flight.time',
    'jump.height'
  ),
  frame = NA
)
output$frame[1] <- Jump.1$Frame[start.unweighting]
output$frame[2] <- Jump.1$Frame[end.unweighting]
output$frame[3] <- Jump.1$Frame[start.braking]
output$frame[4] <- Jump.1$Frame[end.braking]
output$frame[5] <- Jump.1$Frame[start.propulsion]
output$frame[6] <- Jump.1$Frame[end.propulsion]
output$frame[7] <- Jump.1$Frame[start.flight]
output$frame[8] <- Jump.1$Frame[end.flight]
output$frame[9] <- Flight.time
output$frame[10] <- Jump.height
show(output)


write.csv(output,
          file = paste0('output-', sub('.xlsx', '.csv', filename)))

0 个答案:

没有答案