Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to find the mean height of participants that are present in the cystfibr package of the ISwR library. When printing the entire height data set it appears to have a 21x2 matrix with height values and a 1 or 2 to indicate sex. But, ncol returns a value of NA suggesting it is a vector. Trying to get specific indexes of the matrix (heightdata[1,]) also returns an incorrect number of dimensions error.

I'm looking, to sum up only the height values in the vector but when I tried to execute the code I get the sum of the male and female integers. (25)

install.packages("ISwR")

library(ISwR)

attach(cystfibr)

heightdata = table(height)

print(heightdata)

print(sum(heightdata))

1 Answer

0 votes
by (108k points)

Let me tell you that, the table() from R programming will only provide you the count of values in the vector.

If you want to sum the output of height from heightdata, they are stored in names of heightdata but it is in character format, so first convert it to numeric and then calculate the sum.

sum(as.numeric(names(heightdata)))

#[1] 3177

which is similar to adding the unique values of height.

sum(unique(cystfibr$height))

#[1] 3177

Browse Categories

...