# Rownames in Matrix renames itself

Dear Community,

it would be great, if you could help me out.

I was given some information about people (age, size, weigh, etc.). I put the data into the matrix "socio".
I then wanted to add the Body Mass Index (BMI) to the matrix, but everytime i do this, the riwnames of the matrix change from [1],[2],[3],[4] to "Gewicht" and I don't know why.

Maybe you can help me out. Please see the code below.

Kind regards,
Stephan

#Erstellen der Vektoren

Alter<- c(15,27,56,43)
Groeße<- c(1.69,1.74,1.98,1.88)
Gewicht<- c(65,71,88,91)
Schuhgroeße<- c(35,39,47,45)

#Erstellen der Matrix "socio"

socio<- cbind(Alter,Groeße,Gewicht,Schuhgroeße)

#Berechnen der BMIs jeder Person

BMIPerson1<- socio[1,3]/(socio[1,2]^2)
BMIPerson2<- socio[2,3]/(socio[2,2]^2)
BMIPerson3<- socio[3,3]/(socio[3,2]^2)
BMIPerson4<- socio[4,3]/(socio[4,2]^2)

BMI<- c(BMIPerson1,BMIPerson2,BMIPerson3,BMIPerson4)

socio<- cbind(socio,BMI)

socio

Just get rid of the BMI names

``````Alter<- c(15,27,56,43)
Groeße<- c(1.69,1.74,1.98,1.88)
Gewicht<- c(65,71,88,91)
Schuhgroeße<- c(35,39,47,45)

socio <- cbind(Alter,Groeße,Gewicht,Schuhgroeße)

BMIPerson1<- socio[1,3]/(socio[1,2]^2)
BMIPerson2<- socio[2,3]/(socio[2,2]^2)
BMIPerson3<- socio[3,3]/(socio[3,2]^2)
BMIPerson4<- socio[4,3]/(socio[4,2]^2)

BMI <- c(BMIPerson1,BMIPerson2,BMIPerson3,BMIPerson4)
names(BMI) <- NULL

socio <- cbind(socio,BMI)
socio
#>      Alter Groeße Gewicht Schuhgroeße      BMI
#> [1,]    15   1.69      65          35 22.75831
#> [2,]    27   1.74      71          39 23.45092
#> [3,]    56   1.98      88          47 22.44669
#> [4,]    43   1.88      91          45 25.74694
``````

But I recommend using `dplyr` for this sort of things

``````library(dplyr)

socio <- data.frame(
Alter = c(15, 27, 56, 43),
Groeße = c(1.69, 1.74, 1.98, 1.88),
Gewicht = c(65, 71, 88, 91),
Schuhgroeße = c(35, 39, 47, 45)
)

socio %>%
mutate(BMI = Gewicht/(Groeße^2))
#>   Alter Groeße Gewicht Schuhgroeße      BMI
#> 1    15   1.69      65          35 22.75831
#> 2    27   1.74      71          39 23.45092
#> 3    56   1.98      88          47 22.44669
#> 4    43   1.88      91          45 25.74694
``````
3 Likes

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