Take the mean of a columns in csv and create a new row with the information
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am pretty new to R but liked all of the help that was available. The problem I am encountering, is that there have been so many updates since a lot of videos and help sites have been made that the code is no longer valid.
I have a csv of immunization percentages and I want to find the mean of each column of data. If there is a way to add this value as the last row of data that would be ideal but not necessary. I have tried several different types of mean functions but I continue to get error messages.
I have tried the following codes. My file is called Measles1 and the columns are Y followed by the year. I took put the error codes specific to the lines but wanted to show what I have tried for reference. Any help would be greatly appreciated.
> colMeans(Measles1$Y2017)
> colMeans(Measles1)
> mean(Measles1$Y2017)
> mean(Measles1$Y2017, na.rm = TRUE)
> colMeans(Measles1$Y2017, na.rm = TRUE)
> Means <- colMeans(as.numeric(as.character(Measles1)))
> results.mean <- mean(Measles1)
> results.mean <- mean(Measles1,na.rm = TRUE)
> mean(Measles1[2:39])
I am sure that I am just missing something very simple. Thank you for your help.
r row rstudio add mean
add a comment |
I am pretty new to R but liked all of the help that was available. The problem I am encountering, is that there have been so many updates since a lot of videos and help sites have been made that the code is no longer valid.
I have a csv of immunization percentages and I want to find the mean of each column of data. If there is a way to add this value as the last row of data that would be ideal but not necessary. I have tried several different types of mean functions but I continue to get error messages.
I have tried the following codes. My file is called Measles1 and the columns are Y followed by the year. I took put the error codes specific to the lines but wanted to show what I have tried for reference. Any help would be greatly appreciated.
> colMeans(Measles1$Y2017)
> colMeans(Measles1)
> mean(Measles1$Y2017)
> mean(Measles1$Y2017, na.rm = TRUE)
> colMeans(Measles1$Y2017, na.rm = TRUE)
> Means <- colMeans(as.numeric(as.character(Measles1)))
> results.mean <- mean(Measles1)
> results.mean <- mean(Measles1,na.rm = TRUE)
> mean(Measles1[2:39])
I am sure that I am just missing something very simple. Thank you for your help.
r row rstudio add mean
Trynew_Measles <- rbind(Measles1, colMeans(Measles1))
– Ronak Shah
Nov 29 '18 at 3:46
add a comment |
I am pretty new to R but liked all of the help that was available. The problem I am encountering, is that there have been so many updates since a lot of videos and help sites have been made that the code is no longer valid.
I have a csv of immunization percentages and I want to find the mean of each column of data. If there is a way to add this value as the last row of data that would be ideal but not necessary. I have tried several different types of mean functions but I continue to get error messages.
I have tried the following codes. My file is called Measles1 and the columns are Y followed by the year. I took put the error codes specific to the lines but wanted to show what I have tried for reference. Any help would be greatly appreciated.
> colMeans(Measles1$Y2017)
> colMeans(Measles1)
> mean(Measles1$Y2017)
> mean(Measles1$Y2017, na.rm = TRUE)
> colMeans(Measles1$Y2017, na.rm = TRUE)
> Means <- colMeans(as.numeric(as.character(Measles1)))
> results.mean <- mean(Measles1)
> results.mean <- mean(Measles1,na.rm = TRUE)
> mean(Measles1[2:39])
I am sure that I am just missing something very simple. Thank you for your help.
r row rstudio add mean
I am pretty new to R but liked all of the help that was available. The problem I am encountering, is that there have been so many updates since a lot of videos and help sites have been made that the code is no longer valid.
I have a csv of immunization percentages and I want to find the mean of each column of data. If there is a way to add this value as the last row of data that would be ideal but not necessary. I have tried several different types of mean functions but I continue to get error messages.
I have tried the following codes. My file is called Measles1 and the columns are Y followed by the year. I took put the error codes specific to the lines but wanted to show what I have tried for reference. Any help would be greatly appreciated.
> colMeans(Measles1$Y2017)
> colMeans(Measles1)
> mean(Measles1$Y2017)
> mean(Measles1$Y2017, na.rm = TRUE)
> colMeans(Measles1$Y2017, na.rm = TRUE)
> Means <- colMeans(as.numeric(as.character(Measles1)))
> results.mean <- mean(Measles1)
> results.mean <- mean(Measles1,na.rm = TRUE)
> mean(Measles1[2:39])
I am sure that I am just missing something very simple. Thank you for your help.
r row rstudio add mean
r row rstudio add mean
asked Nov 29 '18 at 3:43
SStephanieSStephanie
91
91
Trynew_Measles <- rbind(Measles1, colMeans(Measles1))
– Ronak Shah
Nov 29 '18 at 3:46
add a comment |
Trynew_Measles <- rbind(Measles1, colMeans(Measles1))
– Ronak Shah
Nov 29 '18 at 3:46
Try
new_Measles <- rbind(Measles1, colMeans(Measles1))– Ronak Shah
Nov 29 '18 at 3:46
Try
new_Measles <- rbind(Measles1, colMeans(Measles1))– Ronak Shah
Nov 29 '18 at 3:46
add a comment |
2 Answers
2
active
oldest
votes
It would give us a better idea if you could provide a representative sample. You need to make sure that all columns are numeric in order to compute their means at once. One way to check this would be str(your_data_frame).
Using the built-in mtcars dataset:
# na.rm argument is optional depending on your data
mtcars[nrow(mtcars) + 1, ] <- colMeans(mtcars, na.rm = T)
@Ronak Shah's recommendation works well, too:
mtcars <- rbind(mtcars, colMeans(mtcars, na.rm = T))
you were correct the data was not being stored as numeric. I fixed that and tried to use the code and realized the first column are the country names instead of being the titles of the rows. Once I fixed all of this the code worked perfect.
– SStephanie
Nov 30 '18 at 4:08
add a comment |
This code will provide you option of finding grand total or mean by column values
d1 <- data_frame(
name = c("jim", "john", "jim", "john"),
`2012` = c(57, 58, 47, 57),
`2013` = c(14, 3, 3, 90))
library(tidyverse)
d1 <-bind_rows(d1,
d1 %>%
group_by(name) %>%
summarise_all(funs(mean)) %>%
mutate(name = paste0(name, '_total')))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531533%2ftake-the-mean-of-a-columns-in-csv-and-create-a-new-row-with-the-information%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It would give us a better idea if you could provide a representative sample. You need to make sure that all columns are numeric in order to compute their means at once. One way to check this would be str(your_data_frame).
Using the built-in mtcars dataset:
# na.rm argument is optional depending on your data
mtcars[nrow(mtcars) + 1, ] <- colMeans(mtcars, na.rm = T)
@Ronak Shah's recommendation works well, too:
mtcars <- rbind(mtcars, colMeans(mtcars, na.rm = T))
you were correct the data was not being stored as numeric. I fixed that and tried to use the code and realized the first column are the country names instead of being the titles of the rows. Once I fixed all of this the code worked perfect.
– SStephanie
Nov 30 '18 at 4:08
add a comment |
It would give us a better idea if you could provide a representative sample. You need to make sure that all columns are numeric in order to compute their means at once. One way to check this would be str(your_data_frame).
Using the built-in mtcars dataset:
# na.rm argument is optional depending on your data
mtcars[nrow(mtcars) + 1, ] <- colMeans(mtcars, na.rm = T)
@Ronak Shah's recommendation works well, too:
mtcars <- rbind(mtcars, colMeans(mtcars, na.rm = T))
you were correct the data was not being stored as numeric. I fixed that and tried to use the code and realized the first column are the country names instead of being the titles of the rows. Once I fixed all of this the code worked perfect.
– SStephanie
Nov 30 '18 at 4:08
add a comment |
It would give us a better idea if you could provide a representative sample. You need to make sure that all columns are numeric in order to compute their means at once. One way to check this would be str(your_data_frame).
Using the built-in mtcars dataset:
# na.rm argument is optional depending on your data
mtcars[nrow(mtcars) + 1, ] <- colMeans(mtcars, na.rm = T)
@Ronak Shah's recommendation works well, too:
mtcars <- rbind(mtcars, colMeans(mtcars, na.rm = T))
It would give us a better idea if you could provide a representative sample. You need to make sure that all columns are numeric in order to compute their means at once. One way to check this would be str(your_data_frame).
Using the built-in mtcars dataset:
# na.rm argument is optional depending on your data
mtcars[nrow(mtcars) + 1, ] <- colMeans(mtcars, na.rm = T)
@Ronak Shah's recommendation works well, too:
mtcars <- rbind(mtcars, colMeans(mtcars, na.rm = T))
answered Nov 29 '18 at 4:02
Ozan147Ozan147
2,0821519
2,0821519
you were correct the data was not being stored as numeric. I fixed that and tried to use the code and realized the first column are the country names instead of being the titles of the rows. Once I fixed all of this the code worked perfect.
– SStephanie
Nov 30 '18 at 4:08
add a comment |
you were correct the data was not being stored as numeric. I fixed that and tried to use the code and realized the first column are the country names instead of being the titles of the rows. Once I fixed all of this the code worked perfect.
– SStephanie
Nov 30 '18 at 4:08
you were correct the data was not being stored as numeric. I fixed that and tried to use the code and realized the first column are the country names instead of being the titles of the rows. Once I fixed all of this the code worked perfect.
– SStephanie
Nov 30 '18 at 4:08
you were correct the data was not being stored as numeric. I fixed that and tried to use the code and realized the first column are the country names instead of being the titles of the rows. Once I fixed all of this the code worked perfect.
– SStephanie
Nov 30 '18 at 4:08
add a comment |
This code will provide you option of finding grand total or mean by column values
d1 <- data_frame(
name = c("jim", "john", "jim", "john"),
`2012` = c(57, 58, 47, 57),
`2013` = c(14, 3, 3, 90))
library(tidyverse)
d1 <-bind_rows(d1,
d1 %>%
group_by(name) %>%
summarise_all(funs(mean)) %>%
mutate(name = paste0(name, '_total')))
add a comment |
This code will provide you option of finding grand total or mean by column values
d1 <- data_frame(
name = c("jim", "john", "jim", "john"),
`2012` = c(57, 58, 47, 57),
`2013` = c(14, 3, 3, 90))
library(tidyverse)
d1 <-bind_rows(d1,
d1 %>%
group_by(name) %>%
summarise_all(funs(mean)) %>%
mutate(name = paste0(name, '_total')))
add a comment |
This code will provide you option of finding grand total or mean by column values
d1 <- data_frame(
name = c("jim", "john", "jim", "john"),
`2012` = c(57, 58, 47, 57),
`2013` = c(14, 3, 3, 90))
library(tidyverse)
d1 <-bind_rows(d1,
d1 %>%
group_by(name) %>%
summarise_all(funs(mean)) %>%
mutate(name = paste0(name, '_total')))
This code will provide you option of finding grand total or mean by column values
d1 <- data_frame(
name = c("jim", "john", "jim", "john"),
`2012` = c(57, 58, 47, 57),
`2013` = c(14, 3, 3, 90))
library(tidyverse)
d1 <-bind_rows(d1,
d1 %>%
group_by(name) %>%
summarise_all(funs(mean)) %>%
mutate(name = paste0(name, '_total')))
answered Nov 29 '18 at 4:14
HunaidkhanHunaidkhan
1,024516
1,024516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531533%2ftake-the-mean-of-a-columns-in-csv-and-create-a-new-row-with-the-information%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
new_Measles <- rbind(Measles1, colMeans(Measles1))– Ronak Shah
Nov 29 '18 at 3:46