如何将一段 R 代码应用于数据框的每一列
How can I apply a piece of R code to every column of my data frame
我要分析EMG数据,但是我不太会用R:
我有一个包含 9 列的 data.frame:一列指定时间,另外 8 列指定我的频道。
我想过滤我的 emg 数据,但我只能对每个通道执行此操作,但我想一次对数据帧的所有通道执行此操作,因此我不必将其应用于每个通道。
# This example computes the LE-envelope using the lowpass routine
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw$channel1, samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the
# LE-envelope
plot(x, channel = 1, main = "Original channel")
plot(x_rect, main = "Rectified channel")
plot(y, main = "LE-envelope")
# reset graphical parameters
par(op)
所以我可以不在这里使用 extensor_raw$channel1 而是放入 extensor_raw$i 之类的东西并绕过它吗?或者有什么方法可以将这段代码应用于每个通道(即 9 列数据帧中的 8 列,不包括指定时间的第一列?)
如果是按列,则使用lapply
并存储为list
,并假设所有列都需要更改。 (注意这里没有测试,plot
里面的par
可能要改)
lst1 <- lapply(extensor_raw, \(vec) {
x <- as.emg(vec, samplingrate = 1000, units = "mV")
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the
# LE-envelope
plot(x, channel = 1, main = "Original channel")
plot(x_rect, main = "Rectified channel")
plot(y, main = "LE-envelope")
# reset graphical parameters
par(op)
})
这是我的解决方案。首先,由于您的问题没有数据,我使用了 UCI 机器学习存储库中的 'EMG data for gestures Data Set'。
Link https://archive.ics.uci.edu/ml/datasets/EMG+data+for+gestures
它与您一直使用的数据集非常相似,第一个变量是时间,然后是 8 个变量是通道,最后一个是 class
要为每个通道创建图表,您可以使用 FOR 循环,将您关注的列用作迭代运算符。中间代码与您的代码相同,最后在绘图时我更改了绘图标题,使其与其各自的列名称相似。
library(biosignalEMG)
extensor_raw <- read.delim("01/1_raw_data_13-12_22.03.16.txt")
head(extensor_raw)
for(i in names(extensor_raw[2:9])){
print(paste("Drawing for ", i))
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the
# LE-envelope
plot(x, channel = 1, main = paste("Original ", i))
plot(x_rect, main = paste("Rectified", i))
plot(y, main = paste("LE-envelope", i))
}
在此代码的末尾,您可以看到在 rstudio 的图表部分创建了多个页面,同时绘制了从 1 到 8 的每个通道
喜欢第 5 频道,其他频道也一样。我希望这可以帮助您解决问题。
关于您在评论中提出的第二部分:如果您有单独的文件,我们将其分开。将一一阅读,然后绘制。为此,我们将使用嵌套的 FOR 循环。
首先设置你的工作目录,你有所有的手势文件。就像我的例子一样,我的目录中有两个具有相同结构的文件。
代码改动如下:
setwd('~/Downloads/EMG_data_for_gestures-master/01')
library(biosignalEMG)
for(j in list.files()){
print(paste("reading file ",j))
extensor_raw <- read.delim(j)
head(extensor_raw)
for(i in names(extensor_raw[2:9])){
print(paste("Drawing for ", i))
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the LE-envelope
plot(x, channel = 1, main = paste("Original ", i," from ", j))
plot(x_rect, main = paste("Rectified", i," from ", j))
plot(y, main = paste("LE-envelope", i," from ", j))
}
}
希望对您有所帮助。
我要分析EMG数据,但是我不太会用R: 我有一个包含 9 列的 data.frame:一列指定时间,另外 8 列指定我的频道。 我想过滤我的 emg 数据,但我只能对每个通道执行此操作,但我想一次对数据帧的所有通道执行此操作,因此我不必将其应用于每个通道。
# This example computes the LE-envelope using the lowpass routine
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw$channel1, samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the
# LE-envelope
plot(x, channel = 1, main = "Original channel")
plot(x_rect, main = "Rectified channel")
plot(y, main = "LE-envelope")
# reset graphical parameters
par(op)
所以我可以不在这里使用 extensor_raw$channel1 而是放入 extensor_raw$i 之类的东西并绕过它吗?或者有什么方法可以将这段代码应用于每个通道(即 9 列数据帧中的 8 列,不包括指定时间的第一列?)
如果是按列,则使用lapply
并存储为list
,并假设所有列都需要更改。 (注意这里没有测试,plot
里面的par
可能要改)
lst1 <- lapply(extensor_raw, \(vec) {
x <- as.emg(vec, samplingrate = 1000, units = "mV")
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the
# LE-envelope
plot(x, channel = 1, main = "Original channel")
plot(x_rect, main = "Rectified channel")
plot(y, main = "LE-envelope")
# reset graphical parameters
par(op)
})
这是我的解决方案。首先,由于您的问题没有数据,我使用了 UCI 机器学习存储库中的 'EMG data for gestures Data Set'。
Link https://archive.ics.uci.edu/ml/datasets/EMG+data+for+gestures
它与您一直使用的数据集非常相似,第一个变量是时间,然后是 8 个变量是通道,最后一个是 class
要为每个通道创建图表,您可以使用 FOR 循环,将您关注的列用作迭代运算符。中间代码与您的代码相同,最后在绘图时我更改了绘图标题,使其与其各自的列名称相似。
library(biosignalEMG)
extensor_raw <- read.delim("01/1_raw_data_13-12_22.03.16.txt")
head(extensor_raw)
for(i in names(extensor_raw[2:9])){
print(paste("Drawing for ", i))
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the
# LE-envelope
plot(x, channel = 1, main = paste("Original ", i))
plot(x_rect, main = paste("Rectified", i))
plot(y, main = paste("LE-envelope", i))
}
在此代码的末尾,您可以看到在 rstudio 的图表部分创建了多个页面,同时绘制了从 1 到 8 的每个通道
喜欢第 5 频道,其他频道也一样。我希望这可以帮助您解决问题。
关于您在评论中提出的第二部分:如果您有单独的文件,我们将其分开。将一一阅读,然后绘制。为此,我们将使用嵌套的 FOR 循环。 首先设置你的工作目录,你有所有的手势文件。就像我的例子一样,我的目录中有两个具有相同结构的文件。
代码改动如下:
setwd('~/Downloads/EMG_data_for_gestures-master/01')
library(biosignalEMG)
for(j in list.files()){
print(paste("reading file ",j))
extensor_raw <- read.delim(j)
head(extensor_raw)
for(i in names(extensor_raw[2:9])){
print(paste("Drawing for ", i))
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the LE-envelope
plot(x, channel = 1, main = paste("Original ", i," from ", j))
plot(x_rect, main = paste("Rectified", i," from ", j))
plot(y, main = paste("LE-envelope", i," from ", j))
}
}
希望对您有所帮助。