0

As a part of the Recommender systems course at Coursera, I am doing assignments in R (https://github.com/eponkratova/projects-recommender-system/blob/master/recommender_knit.Rmd) and so far I got a N result.

Is there a way to rename col (var renamed_mean_1) more elegantly during the step where I calculate the average by a column (var dataset_mean_1)?

install.packages('gsheet', repos="http://cran.rstudio.com/")
library('gsheet')
url <- 'https://docs.google.com/spreadsheets/d/1XDBRCYFTxsw27AivxJ5pWxDHN0WA6GqSP46PVe2BCQ4/edit?usp=sharing'
dataset <- gsheet2tbl(url)
dataset_mean_1 <- data.frame(colMeans(dataset, na.rm = TRUE))
install.packages('plyr', repos="https://cran.r-project.org")
library('plyr')
renamed_mean_1 <- rename(dataset_mean_1,c('colMeans.dataset..na.rm...TRUE.'='Mean'))
ordered_mean_1 <- head(renamed_mean_1[order(-renamed_mean_1$Mean),,drop=FALSE],n=4)

I don't have much experience with R, and for this reason, my code is a bit bulky.

Could you please help me?

1
  • You can reset column names using colnames() function. Commented Jan 26, 2017 at 18:07

1 Answer 1

1

Try this:

dataset_mean_1 <- data.frame(colMeans(dataset, na.rm = TRUE))
colnames(dataset_mean_1) <- "renamed_mean_1"

Or just to one call:

dataset_mean_1 <- data.frame(renamed_mean_1 =colMeans(dataset, na.rm = TRUE))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.