understanding sapply function in r

0 votes
This may be the most inane question ever, but I'm having trouble understanding the function sapply: Here's the problem:

Example:

d stands for matrix (1:10, 5,2)

NA for d[3].

# [,1] [,2] [,3] [,4] [,5] [,6]

1 6 #[1,] #[1,] #[1,] #[1,] #

2 7 #[2,] #[2,] #[2,] #[2,] #

#[3,] NA #[3,] NA #[3,] NA #[3,]

##[4,] ##########################

#[5,] #[5,] #[5,] #[5,] #[5,

If I wanted to utilise the sapply function to calculate the row means, I'd do something like this:

sapply(d,mean)

#[1] NA 4 5 6 7 8 9 10 NA 4 5 6 7 8 9 10 NA 4 5 6 7 8 9 10 NA 4 5 6 7 8 9

Shouldn't it give me the average of the items' list? Instead of the mean, it just throws out the elements of my matrix.

I receive the correct response when I use apply:

apply(d,1,mean, na.rm=T, na.rm=T, na.rm=T, na.rm

[1] 3.5 4.5 8.0 6.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5

Is anyone willing to explain themselves to me?
Jun 19 in Data Analytics by Avinash
• 1,260 points
6 views

1 answer to this question.

0 votes
Do you read 2? sapply. Either a vector or an expression object may be passed to the function. A matrix cannot be accepted. As a result, the matrix d in your example is treated as a vector of numbers:

sapply(as.numeric(d),mean)
answered 4 days ago by Sohail
• 2,280 points

Related Questions In Data Analytics

0 votes
2 answers

R function for finding the index of an element in a vector?

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 12, 2020 in Data Analytics by Rajiv
• 8,910 points
51,630 views
+1 vote
2 answers

Which function can I use to clear the console in R and RStudio ?

Description                   Windows & Linux           Mac Clear console                      Ctrl+L ...READ MORE

answered Apr 17, 2018 in Data Analytics by anonymous
46,823 views
0 votes
1 answer

Why should I use set.seed function() in R?

set.seed(seed) Set the seed of R‘s random number ...READ MORE

answered Apr 24, 2018 in Data Analytics by zombie
• 3,790 points
797 views
0 votes
1 answer

Python equivalent to replace function in R

There is actually a replace function in ...READ MORE

answered May 31, 2018 in Data Analytics by Bharani
• 4,660 points
750 views
0 votes
1 answer

How to add external markers in maps?

Use external markers by creating icons using ...READ MORE

answered Oct 14, 2019 in Data Analytics by Cherukuri
• 33,010 points
241 views
0 votes
0 answers

Add multiple maps over each other

How to multiple layers of maps over ...READ MORE

Sep 19, 2019 in Data Analytics by anonymous
• 33,010 points
271 views
0 votes
1 answer

Reduce markers overlapping in maps

Hey, You can either reduce the font size ...READ MORE

answered Sep 23, 2019 in Data Analytics by anonymous
• 3,450 points
1,036 views
0 votes
1 answer

How to create and call groups in maps?

Groups are used to group a set ...READ MORE

answered Oct 14, 2019 in Data Analytics by Cherukuri
• 33,010 points
164 views
0 votes
1 answer

How to use Aggregate function in R

Where dat is the name of your ...READ MORE

answered 4 days ago in Data Analytics by Sohail
• 2,280 points
9 views
0 votes
1 answer

How can I create a correlation matrix in R?

An example, d <- data.frame(x1=rnorm(10), ...READ MORE

answered Jun 14 in Data Analytics by Sohail
• 2,280 points
10 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP