Hi! I'm trying to merge subjects from a dataframe that have the same number on a column.
> ID CONSENTIMIENTO EDAD GENERO FECHA.NAC JPC_VI_GRUPO JPC_VI_COND JPC_VI_COLOR JPC_VI_DECISION1 JPC_VI_DECISION2
> 1 1 1 11 2 30/05/2006 11 D N 1 0
> 2 7 1 43 1 10/03/1974 11 D V 3 3
> 3 3 1 11 2 24/05/2006 33 D <NA> 4 5
> 4 6 1 41 2 05/08/1976 33 D <NA> 4 4
> 5 8 1 5 1 28/04/2012 1 D V 1 2
> 6 11 1 38 1 27/07/1979 1 D N 3 3
> 7 9 1 8 2 08/05/2009 18 D V 1 3
> 8 10 1 36 2 24/06/1981 18 D N 3 4
> 9 16 1 8 1 03/02/2009 19 D V 1 0
> 10 17 1 36 2 12/05/1981 19 D N 2 4
in this case i need to merge rows that have the same JPC_VI_GRUPO number to make a grid.table with JPC_VI_COND and JPC_VI_DECISION1
> grid.table(table(var_datosgrupal$JPC_VI_COND,var_datosgrupal$JPG_VG_VINCULO))
Here i have the total amount of subjects but i want to make a grid using only the peer that have the same group number.
How could i get this done?
Can you provide a reproducible example?
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
Have you tried adding a third variable to table
table (a, b, c)
Should effectively give you a
A= ...
BxC
As a table?
You may find it's easier to use ftable
Sure,
> df <- data.frame(stringsAsFactors = FALSE,
> ID = c(1,2,3,4,5,6,7,8),
> JPC_VI_GRUPO = c(1,1,2,2,3,3,4,4)
> JPC_VI_COND=c(D,D,D,I,I,D,I,I)
> JPC_VI_DECISION1=c(1,3,4,2,1,4,3,1)
here i have an example of the df im trying to use
i used grid.table to represent the data for every subject in my original df but now i need to use JPC_VI_GRUPO.
> grid.table(table(var_datosgrupal$JPC_VI_COND,var_datosgrupal$JPG_VG_VINCULO))
Hope this is a reproducible example
1 Like
What exactly do you want the output to look like?
I have this, but I am not sure if that is what you want:
library(dplyr)
df <- data.frame(stringsAsFactors = FALSE,
ID = c(1,2,3,4,5,6,7,8),
JPC_VI_GRUPO = c(1,1,2,2,3,3,4,4),
JPC_VI_COND=c("D","D","D","I","I","D","I","I"),
JPC_VI_DECISION1=c(1,3,4,2,1,4,3, 1))
df %>%
group_by(JPC_VI_GRUPO, JPC_VI_COND) %>%
count() %>%
pivot_wider(JPC_VI_COND, names_from = c(JPC_VI_GRUPO), values_from = n)
# A tibble: 2 x 5
# Groups: JPC_VI_COND [2]
JPC_VI_COND `1` `2` `3` `4`
<chr> <int> <int> <int> <int>
1 D 2 1 1 NA
2 I NA 1 1 2
Please use this as an example.
> df <- data.frame(stringsAsFactors = FALSE,
> ID = c(1,2,3,4,5,6,7,8),
> JPC_VI_GRUPO = c(1,1,2,2,3,3,4,4),
> JPC_VI_COND=c("D","D","I","I","D","D","I","I"),
> JPC_VI_DECISION1=c(1,3,4,2,1,4,3,1))
What i want is to show how many dyads of "D" and "I" conditions have responded 1, 2, 3 or 4 in decision.
So if my n was 8 (because i had 8 subjects) now my n should be 4 (because they where in peer).
I don't understand what you want the output to be.
Can you create it?
> df <- data.frame(stringsAsFactors = FALSE,
> ID = c(1,2,3,4,5,6,7,8),
> JPC_VI_GRUPO = c(1,1,2,2,3,3,4,4),
> JPC_VI_COND=c("D","D","I","I","D","D","I","I"),
> JPC_VI_DECISION1=c(1,1,4,4,2,2,3,3))
i would need to count the amount of JPC_VI_GRUPO that meet both condition JPC_VI_COND and JPC_VI_DECISION1
so in this table we should see "1" in every slot
i hope this makes sense
But with that data, there isn't a 1 in every slot. for example, in group 1, they are both D
.
df %>%
group_by(JPC_VI_GRUPO, JPC_VI_COND) %>%
count() %>%
pivot_wider(JPC_VI_COND, names_from = c(JPC_VI_GRUPO), values_from = n)
# A tibble: 2 x 5
# Groups: JPC_VI_COND [2]
JPC_VI_COND `1` `2` `3` `4`
<chr> <int> <int> <int> <int>
1 D 2 NA 2 NA
2 I NA 2 NA 2
i think my point is that i need some code to merge the 2 rows (i.e. 1 and 2) that represent the same group so i can count how many groups i have that meet JPC_VI_COND and JPC_VI_DECISION1
system
Closed
March 5, 2021, 2:29am
12
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.