I have a data frame in R which is similar to the following types. Actually my actual "DFA" ??? Dataframe is bigger than here, but I really do not want to confuse anyone so that's why I try to make things as simple as possible.
So here is the data frame.
id & lt; -c (1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3 , 3,3,3,3,3,3,3) a & lt; -k (3,1,3,3,1,3,3,3,1,3,2,1,2,1,3,3,2,1,1,1,3,1,3 , 3,3,1,1,1,3) B & LT; -C (3,2, 1,1,1,1,1,1,1,1,1,2,1,3,2,1,1,1,1,1,1,1,2,2,2 , 1,3,3, 2,3,2) C & lt; -C (1,3,2,3,2,1,2,3,3,2,2,3,1,2,3,3,1,1,1,2,3,1,1,2 , 2,3,2,2,3,2) D & LT; -C (3,3,3,1,3,2,2,2,2,2,2, 2,2,1,3,1,2,2,3,2,3,2,3,2 , 1,1,1,1,1,2) E & lt; -C (2,3,1, 2,1,2,3,3,1,1,2,1,1,3,3,2,1,1,3,3,2,2,3,3 , 3,2,3,2, 1,3) DF & lt; What I would like to do is to get the events in each column (A, B, C, D, D, E) and for each ID group (1,2,3) (see my column for this later grouping) ???? Then, for columns ???? AA ??? And for ID number ?????? (The latter to see the column) "code" would be something like this:
as.numeric (table (df [1: 10,2])) ## Results: [1] 3 7 Just to briefly explain my results: "???? aa" in the column ??? (And only about those records, who have number one, 'column' is in '1A') We can say that the number '1' happened 3 times and the number '3' 7 happened.
Then, just to show you another example For columns and for ID number ???? 2a ?? (See column 'A' ida '????) for later grouping:
as.numeric (table (DF [11: 20,2]) ## run The results of the code are: [1] 4 3 3 Let me explain a bit again: "A" in the column ??? And only about those observations, which have the number one ???? 2 says 'A' in column). We can say that the number '1' happened four times, the number was '2' 3 times and the number '3' is 3 times.
So what I would like to do is to calculate the incidence of numbers for each custom-defined subset (and then collect these values in a data frame) I know that this is not a difficult task, But the problem is that Iâ € ™ m need to change the input ???? Dfâ ???? Dataframe on a regular basis and therefore the total number of both rows and columns can change over time. |
What have I done so far that I have separated from a different one. ? Specify the dataframe by column, like this:
for (in z ((2: ncol (df)) (paste ("df", z, sep = "."), Df [Z]) will then refer to df.2, df $ a, df.3 is equal to df $ b, df.4 would be equal df $ c etc. But IA ??? ? I am really stuck now and I do not know what is the way to move forward ?? |
Is there any proper, "automatic" way to solve this problem?
How about -
& gt; Library (resize) & Gt; Dftab & lt; - Rhythm (Melt (df, 'id')) gt; dftab, value = 1 variable id abcde 1 3 8 2 2 4 2 1 6 1 2 3 4 2 1 5 1, value = 2 variable ID ABCD1 0 1 4 3 3 2 3 3 3 6 2 3 1 4 5 3 4, Value = 3 variable ID ABCD 1 7 1 4 5 3 2 3 1 4 2 4 3 5 4 4 2 5 Then the number of '3 columns To get 'A' and group '1' you can just do > Dftab [3, 'A', 1] [1] 4
Comments
Post a Comment