如何创建具有 3 个因素的 data.frame?

发布于 2024-12-08 08:20:05 字数 337 浏览 0 评论 0原文

我希望你不会觉得我的问题太愚蠢,我做了很多研究,但似乎我不知道如何解决这个非常烦人的问题。

嗯,我有一个实验中 6 名参与者 (P) 的数据,每个参与者有 50 次试验 (T),有 10 个条件 (C)。所以我想在 r 中创建一个数据框,允许我放置这些数据。 该 data.frame 应具有 3 个因素(P、T 和 C),因此总行数为 (P*T*C)。对我来说,创建这个数据的困难在于,因为我有 6 个参与者的数据,包含 100 个 obs(T) x 10 个变量(C) 的 6 个数据帧。 我想首先使用这些因素创建空数据集,然后根据因素 P、T 和 C 复制 6 个数据集的值。 任何帮助将不胜感激,我是 r 新手。

谢谢。

I hope you won't find my question too silly, i did a lot of research but it seems that i can't figure how to solve this really annoying issue.

Well, i have datas for 6 participants (P) in an experiment, with 50 trials (T) per participants and 10 condition (C). So i'd like to create a dataframe in r allowing me to put these datas.
This data.frame should have 3 factors (P, T and C) and so a number of total row of (P*T*C). The difficulty for me is to create this one, since i have the datas for the 6 participant in 6 data.frame of 100 obs(T) by 10 varibles(C).
I'd like first to create the empty dataset with these factors, and then copy the values of the 6 data.set according to the factors P, T and C.
Any help would be greatly appreciated, i'm novice in r.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-12-15 08:20:05

好的;首先,我们为所有参与者创建一个大数据框:

result<-rbind(dfrforparticipant1, dfrforparticipant2,...dfrforparticipant6) #you'll have to fill out the proper names of the original data.frames

接下来,我们为参与者 ID 添加一列:

numTrials<-50 #although 100 is also mentioned in your question
result$P<-as.factor(rep(1:6, each=numTrials))

最后,我们需要从“宽”格式变为“长”格式(我假设您的列名称包含每个参与者的结果)条件称为 C1、C2 等;我还假设您的原始 data.frames 已经包含一个名为 T 的列来表示试验),如下所示(未经测试,因为您没有提供示例数据):

orgcolnames<-paste("C", 1:10, sep="")
result2<-reshape(result, varying=list(orgcolnames), v.names="val", idvar=c("T","P"), timevar="C", times=seq_along(orgcolnames), direction="long")

您现在想要的是在结果2

OK; First we create one big dataframe for all participants:

result<-rbind(dfrforparticipant1, dfrforparticipant2,...dfrforparticipant6) #you'll have to fill out the proper names of the original data.frames

Next, we add a column for the participant ID:

numTrials<-50 #although 100 is also mentioned in your question
result$P<-as.factor(rep(1:6, each=numTrials))

Finally, we need to go from 'wide' format to 'long' format (I'm assuming your column names holding the results for each condition are called C1, C2 etc. ; I'm also assuming your original data.frames already held a column named T to denote the trial), like this (untested, since you did not provide example data):

orgcolnames<-paste("C", 1:10, sep="")
result2<-reshape(result, varying=list(orgcolnames), v.names="val", idvar=c("T","P"), timevar="C", times=seq_along(orgcolnames), direction="long")

What you want is now in result2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文