[英]How to calculate conditional means from .txt files
I'm fairly new to programming and am looking for some guidance.我对编程很陌生,正在寻找一些指导。 Any help is appreciated.任何帮助表示赞赏。
Here's what I'm trying to do: I have a large number of.txt files from a cognitive experiment (Flanker task, if curious) that I need to compute means for based on condition.这就是我想要做的事情:我有大量来自认知实验(Flanker 任务,如果好奇的话)的 .txt 文件,我需要根据条件计算平均值。 The files have no headers and look like below:这些文件没有标题,如下所示:
XXXXX 1 1 675 XXXXXX 1 1 675
XXYXX 0 1 844 XXXXX 0 1 844
YYYYY 1 1 599 YYYY 1 1 599
YYXYY 0 1 902 YYXYY 0 1 902
I would like to compute means for miliseconds (rightmost column; c4) based on the experimental condition (0 or 1; c2).我想根据实验条件(0 或 1;c2)计算毫秒的平均值(最右边的列;c4)。 I would also need the file name of each.txt file (my participant ID) included in the output.我还需要 output 中包含的 each.txt 文件的文件名(我的参与者 ID)。
I'm most familiar with R but really just for data analysis.我最熟悉 R 但实际上只是用于数据分析。 I also have a little experience with Python and Matlab if those (or something else) better suit my needs.如果这些(或其他)更适合我的需求,我对 Python 和 Matlab 也有一点经验。 Again, a point in any direction would be greatly appreciated.再次,任何方向的一点将不胜感激。
Thanks谢谢
The Tidyverse collection of packages specially the dplyr and readr can easy do this task for you on a grammar likely SQL. Tidyverse 软件包集合特别是dplyr和readr可以轻松地为您完成此任务,语法可能为 SQL。
Something like就像是
#loading packages
library(tidyverse)
#importing data
df <- read_delim("file.txt", delim="|", col_names=c("col1", "col2", "col3", "col4"))
#dealing with data
#only mean for col2 == 1
df %>%
filter(col2 == 1) %>%
summarize(mean_exp = mean(col4))
#mean considering grouping by col2
df %>%
group_by(col2) %>%
summarize(mean_exp = mean(col4))
I may suggest you search for cheatsheets available on the links above.我可能会建议您在上面的链接中搜索可用的备忘单。 They are very easy to understand and reproduce the code.它们很容易理解和重现代码。
Here is how you could do it in R:以下是在 R 中的操作方法:
# mimick your text files
cat("XXXXX 1 1 675",file="XXXXX.txt",sep="\n")
cat("XXYXX 0 1 844",file="XXYXX.txt",sep="\n")
cat("YYYYY 1 1 599",file="YYYYY.txt",sep="\n")
cat("YYXYY 0 1 902",file="YYXYY.txt",sep="\n")
# create a list
my_list_txt <- list.files(pattern=".txt")
files_df <- lapply(my_list_txt, function(x) {read.table(file = x, header = F)})
# create a dataframe
df <- do.call("rbind", lapply(files_df, as.data.frame))
# do the group calculation
library(dplyr)
df %>%
group_by(V2) %>%
summarise(mean = mean(V4))
V2 mean
<int> <dbl>
1 0 873
2 1 637
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.