I need to count the trials in an experiment, separately for each subject. I thought about using the function "by" but I could not manage to achieve this. Instead, I tried "split" and I got closer to a solution but still not getting there yet. The following code should create a variable "miniblock" with the trial/miniblock number. split_cong=split(red_congruent,red_congruent$subject_nr) miniblock_cong=lapply(split_cong,seq_along) red_congruent$miniblock=unlist(miniblock_cong) However, I get the following error message: Error in `$<-.data.frame`(`*tmp*`, "miniblock", value = c(1L, 2L, 3L, : replacement has 460 rows, data has 500 Is there a more efficient way to achieve the result? Maybe with "by" or dplyr? Thanks a lot in advance!
Hi Davide,
You wouldn't be dealing with the Stroop test, would you?
stroop.df<-data.frame(subject=rep(paste("S",1:10,sep=""),each=12),
color=rep(c("R","G","B"),40),cong=rep(rep(c("C","I"),each=3),20))
stroop.df$colcong<-paste(stroop.df$color,stroop.df$cong,sep="")
stroop.rc<-stroop.df[stroop.df$colcong=="RC",]
table(stroop.df$subject[stroop.df$colcong=="RC"])
Jim
On Thu, May 4, 2017 at 4:24 AM, Davide Piffer <pifferdavide at gmail.com>
wrote:> I need to count the trials in an experiment, separately for each
> subject. I thought about using the function "by" but I could not
> manage to achieve this. Instead, I tried "split" and I got closer
to a
> solution but still not getting there yet.
> The following code should create a variable "miniblock" with the
> trial/miniblock number.
> split_cong=split(red_congruent,red_congruent$subject_nr)
> miniblock_cong=lapply(split_cong,seq_along)
> red_congruent$miniblock=unlist(miniblock_cong)
>
> However, I get the following error message: Error in
> `$<-.data.frame`(`*tmp*`, "miniblock", value = c(1L, 2L, 3L,
:
> replacement has 460 rows, data has 500
>
> Is there a more efficient way to achieve the result? Maybe with
"by" or dplyr?
>
> Thanks a lot in advance!
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> On May 3, 2017, at 11:24 AM, Davide Piffer <pifferdavide at gmail.com> wrote: >You should look at this result more closely. Its length is not the same length as the number of rows of the target of the attempted assignment.> unlist(miniblock_cong)You might try: red_congruent$miniblock <- ave( red_congruent$subject_nr, red_congruent$subject_nr, FUN=seq_along) `ave` is very useful for delivering vectors with length equal to nrow of a dataframe. Do remember to name the FUN parameter (although I still usually forget). -- David Winsemius Alameda, CA, USA
Thanks David! I tried this but didn't work. Got a bunch of warning messages: Warning messages: 1: In `[<-.factor`(`*tmp*`, i, value = 1:52) : On 4 May 2017 at 02:23, David Winsemius <dwinsemius at comcast.net> wrote:> >> On May 3, 2017, at 11:24 AM, Davide Piffer <pifferdavide at gmail.com> wrote: >> > > You should look at this result more closely. Its length is not the same length as the number of rows of the target of the attempted assignment. > >> unlist(miniblock_cong) > > You might try: > > red_congruent$miniblock <- ave( red_congruent$subject_nr, red_congruent$subject_nr, FUN=seq_along) > > `ave` is very useful for delivering vectors with length equal to nrow of a dataframe. Do remember to name the FUN parameter (although I still usually forget). > > -- > > David Winsemius > Alameda, CA, USA >