如何将一段 R 代码应用于数据框的每一列

我必须分析 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$i 之类的东西并在其周围循环,而不是使用 extensor_raw$channel1 吗?或者有什么方法可以将这段代码应用于每个通道(即 9 列数据帧的 8 列,不包括指定时间的第一列?)

阿努普提普德

这是我的解决方案。首先,由于您的问题没有数据,我使用了 UCI 机器学习存储库中的“手势数据集的 EMG 数据”。

链接https://archive.ics.uci.edu/ml/datasets/EMG+data+for+gestures

它与您使用的数据集非常相似,第一个变量是时间,然后是 8 个变量是通道,最后一个是类

在此处输入图片说明

要为每个通道创建图形,您可以使用 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))
    
  }
}

我希望这会有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将函数应用于R中数据框中的每一列

如何将一段 R 代码转换为 Shiny 应用程序?

如何将函数应用于R中的每一行数据框?

如何将 lme 函数应用于数据框的每一列?

R:将不同条件的函数(Shapiro 测试)应用于数据帧中的每一列

如何将自定义函数应用于数据框的每一列

如何将函数应用于熊猫数据框中一列的每一行?

R将函数应用于数据框的每一行,将结果存储在同一数据框的新列中

将函数应用于pandas中数据框的每一列

将功能应用于数据框的每一列

是否将函数应用于pandas数据框的每一列而没有for循环?

将函数应用于数据框的每一列

熊猫:将了解列类型的函数应用于数据框的每一列

如何将公式应用于 R 数据框列的一部分?

apply()应用于ExcelFile每个数据框的每一列,Pandas

如何将函数应用于数据集的行并获取 R 中每一行的结果

当函数需要多个输入时,如何将函数应用于R中数据帧的每一行?

将 reduce 应用于 R 数据帧中列的每一行,其中包含一个列表

如何将百分比列添加到R中数据框中的每一列

如何将函数应用于 R 中的数据框列?

如何将数据框中的每一列重新缩放为0-100比例?(在r中)

将函数应用于矩阵的每一列

在熊猫中,如何将函数应用于返回两列的每一列

将函数应用于数据库的每一列

将均值和标准差应用于数据表的每一列

将自定义函数应用于R中数据帧中每一行的两列

如何索引列表的第一个元素,并将其应用于R中数据框的每一行?

Pandas:如何将复杂函数应用于数据框的一列,另外两列作为函数的输入?

R - 将函数应用于数据帧的每一行,函数的参数是来自每一行的值