Inserting NA's in case there are not enough observations

3 views
Skip to first unread message

Ayala Allon

unread,
Nov 26, 2014, 9:08:07 AM11/26/14
to israel-r-...@googlegroups.com


Hi,

 

I have the following data frame (this is only the head of the data frame). The ID column is subject (I have more subjects in the data frame, not only subject #99). I want to calculate the mean "rt" by "subject"  and  "condition" only for observations that have z.score (in absolute values) smaller than 1.

 

> head(b)

  subject   rt ac condition     z.score

1      99 1253  1     200_9  1.20862682

2      99 1895  1     102_2  2.95813507

3      99 1049  1      68_1  1.16862102

4      99 1732  1      68_9  2.94415384

5      99  765  1      34_9 -0.63991180

7      99 1016  1      68_2 -0.03191493

 

I know I can to do it using tapply or dcast after subsetting the data:

b1 <- subset(b, abs(z.score) < 1)

b2 <- dcast(b1, subject~condition, mean, value.var = "rt")

 

and then I get:

> b2

 subject      34_1      34_2      34_9      68_1      68_2     68_9     102_1     102_2    102_9     200_1     200_2

1      99 1028.5714  957.5385  861.6818  837.0000  969.7222 856.4000  912.5556  977.7273 858.7800 1006.0000 1015.3684

2    5203  957.8889  815.2500  845.7750  933.0000  893.0000 883.0435  926.0000  879.2778 813.7308  804.2857  803.8125

3    5205 1456.3333 1008.4286  850.7170 1142.4444  910.4706 998.4667  935.2500  980.9167 897.4681 1040.8000  838.7917

4    5306 1022.2000  940.5882  904.6562 1525.0000 1216.0000 929.5167  955.8571  981.7500 902.8913  997.6000  924.6818

5    5307 1396.1250 1217.1111 1044.4038 1055.5000 1115.6000 980.5833 1003.5714 1482.8571 941.4490 1091.5556 1125.2143

6    5308  659.8571  904.2857  966.7755  960.9091 1048.6000 904.5082  836.2000 1753.6667 926.0400  870.2222 1066.6667

     200_9

1 913.2449

2 843.7200

3 819.9710

4 883.4583

5 989.4918

6 930.7500

 

In the example above for b1 each of the subjects had observations that met the subset demands.

However, it can be that for a certain subject I won't have observations after I subset. In this case I want to get NA in b2 for that subject in the specific condition in which he doesn't have observations that meet the subset demands. Does anyone have an idea for a way to do that?

Any help will be greatly appreciated.

Best,

Ayala

Jonathan Rosenblatt

unread,
Nov 26, 2014, 3:59:08 PM11/26/14
to israel-r-user-group
I would definitely begin by using the dplyr package to do it, and then check if the package behaves like you want.


--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jonathan Rosenblatt
www.john-ros.com

Reply all
Reply to author
Forward
0 new messages