 # Define variable row to restructure data (to calculate robust Manova with WRS package)

Dear R community,

I am trying to calculate a robust MANOVA in R, thus I have to restructure my data. In this regard, I have to create a new variable "row" that identifies the scores within each group. For 3 different groups of each 10 participants, it is advised to do it like this:

``````ocdData\$row<-rep(1:10, 3)
#(Field, Miles, & Field, 2012)
``````

In my case, I have 2 different groups with different group sizes 83 and 111 (row 1:83 and 84:194) am not sure how to deal with this to create the new "row" variable to restructure my data.

``````dfrobMan<-df [, c("E", "M", "mo", "S", "s", "Manipulation")]
dfrobMan\$row<-rep(1:194, 1)   #my problem that is not working (Group 2 values appear as NA after melt function)
dfMelt <-melt (dfrobMan, id = c("Manipulation", "row"), measured = c("M",  "E", "S","mo" ,"s"))
´´´

please considere providing us with a small sample of representative data, so that its easy for us to help you. The article gives hints on how to do that, if you are unsure.

Thank you very much. I will try:

``````head(iris, 5)[, c('group', 'Mi', 'S')]
data.frame(  group = c(1, 1, 1, 2,2),
Mi = c(1,5,6,3,2), S = c(4,5,3,6,4))
``````

So for that dataset, I would like to create a new variable "row" that indicates the row.
So regarding my group variables, row should indicate the number of the participant in one group, so it should indicate the different groups 1:3 and 4:5.
Lateron, when using the function melt, row shall indicate where the data will be split and to be restructured.
Does my explanation make sense?

sorry, I cant easily picture where you are starting from and what you are trying to achieve.
There was something about identifiying which rows belong to which of two groups, and providing them as a comma list.
If thats right, this might help.

``````
df_1 <- data.frame(  group = c(1, 1, 1, 2,2),
Mi = c(1,5,6,3,2), S = c(4,5,3,6,4))

df_2 <- df_1 %>% mutate(row=row_number())

distinct_groups <- unique(pull(df_2,group))

if(exists("new_df")){
rm(new_df)
}

for (g in distinct_groups){

temp <- df_2 %>% filter(group==g)
row_vec <- pull(temp,row)

new_row <- tibble(
group=g,
row=paste0(row_vec,collapse = ",")
)

if(!exists("new_df")){
new_df <- new_row
} else {
new_df <- bind_rows(new_df,new_row)
}

}
print(df_1)
print(df_2)
print(new_df)``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.