How to calculate mean value for each column ignoring NA [duplicate]calculate the mean for each column of a matrix in RHow to sort a dataframe by multiple column(s)R: removing rows and replacing values using conditions from multiple columnsAssign values based on NA in other two variables of a data frameR- Consecutive K-means clustering operations in Rcount the number of non-NA numeric values of each row in dplyrComplicated filter statements in dbplyr using lists of listsdata frame set value based on matching specific row name to column nameloop a function in r and output the result to .csv fileconditional lag calculations using RGet minimum value for each group by multiple subgroups

How would you control supersoldiers in a late iron-age society?

I was promised a work PC but still awaiting approval 3 months later so using my own laptop - Is it fair to ask employer for laptop insurance?

What is and what isn't ullage in rocket science?

A medieval fantasy adventurer lights a torch in a 100% pure oxygen room. What happens?

What is the mathematical notation for rounding a given number to the nearest integer?

Bash, import output from command as command

Thematic, genred concepts in Ancient Greek?

Newly created XFS filesystem shows 78 GB used

Some Prime Peerage

Should you only use colons and periods in dialogues?

Why did it become so much more expensive to start a university?

Might have gotten a coworker sick, should I address this?

How are aircraft depainted?

What was redacted in the Yellowhammer report? (Point 15)

What does a Light weapon mean mechanically?

How do certain apps show new notifications when internet access is restricted to them?

Is there a real-world mythological counterpart to WoW's "kill your gods for power" theme?

Why did they ever make smaller than full-frame sensors?

POSIX compatible way to get user name associated with a user ID

2000s space film where an alien species has almost wiped out the human race in a war

why car dealer is insisting on loan v/s cash

Why do sellers care about down payments?

Can I conceal an antihero's insanity - and should I?

Stucturing information on this trade show banner



How to calculate mean value for each column ignoring NA [duplicate]


calculate the mean for each column of a matrix in RHow to sort a dataframe by multiple column(s)R: removing rows and replacing values using conditions from multiple columnsAssign values based on NA in other two variables of a data frameR- Consecutive K-means clustering operations in Rcount the number of non-NA numeric values of each row in dplyrComplicated filter statements in dbplyr using lists of listsdata frame set value based on matching specific row name to column nameloop a function in r and output the result to .csv fileconditional lag calculations using RGet minimum value for each group by multiple subgroups






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0
















This question already has an answer here:



  • calculate the mean for each column of a matrix in R

    6 answers



table, data.frame.



Which has 12 columns with variable names and 24 rows df



Like:



Var1 Var2 Var3 Var4 Var12
1 NA 2 3 4
5 6 2 3 3
NA 7 8 NA 4


And I want to calculate the mean for each column while ignoring the Na's
For example:



colMeans(df)



And get the result like:



Var1 Var2 Var3 Var4 Var12
3 6,5 4 3 3,66


I don't want the NA's to be considered in the calculation of Mean.



I tried some methods like na.omit, !is.na, but I don't get the desired result like what I described above.










share|improve this question
















marked as duplicate by Ronak Shah r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • colMeans(df, na.rm = TRUE)

    – Ronak Shah
    Mar 28 at 10:30











  • Yeah that worked ! Thanx was so easy.. :P

    – Alex Rika
    Mar 28 at 10:32











  • You might wanna post it as answer?

    – Alex Rika
    Mar 28 at 10:33











  • The calculation of Means is correct but how can I get it like horizontal while getting the col names above each mean number?

    – Alex Rika
    Mar 28 at 10:33











  • Not sure what you mean by that. Can you update the post with the expected output?

    – Ronak Shah
    Mar 28 at 10:39


















0
















This question already has an answer here:



  • calculate the mean for each column of a matrix in R

    6 answers



table, data.frame.



Which has 12 columns with variable names and 24 rows df



Like:



Var1 Var2 Var3 Var4 Var12
1 NA 2 3 4
5 6 2 3 3
NA 7 8 NA 4


And I want to calculate the mean for each column while ignoring the Na's
For example:



colMeans(df)



And get the result like:



Var1 Var2 Var3 Var4 Var12
3 6,5 4 3 3,66


I don't want the NA's to be considered in the calculation of Mean.



I tried some methods like na.omit, !is.na, but I don't get the desired result like what I described above.










share|improve this question
















marked as duplicate by Ronak Shah r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • colMeans(df, na.rm = TRUE)

    – Ronak Shah
    Mar 28 at 10:30











  • Yeah that worked ! Thanx was so easy.. :P

    – Alex Rika
    Mar 28 at 10:32











  • You might wanna post it as answer?

    – Alex Rika
    Mar 28 at 10:33











  • The calculation of Means is correct but how can I get it like horizontal while getting the col names above each mean number?

    – Alex Rika
    Mar 28 at 10:33











  • Not sure what you mean by that. Can you update the post with the expected output?

    – Ronak Shah
    Mar 28 at 10:39














0












0








0









This question already has an answer here:



  • calculate the mean for each column of a matrix in R

    6 answers



table, data.frame.



Which has 12 columns with variable names and 24 rows df



Like:



Var1 Var2 Var3 Var4 Var12
1 NA 2 3 4
5 6 2 3 3
NA 7 8 NA 4


And I want to calculate the mean for each column while ignoring the Na's
For example:



colMeans(df)



And get the result like:



Var1 Var2 Var3 Var4 Var12
3 6,5 4 3 3,66


I don't want the NA's to be considered in the calculation of Mean.



I tried some methods like na.omit, !is.na, but I don't get the desired result like what I described above.










share|improve this question

















This question already has an answer here:



  • calculate the mean for each column of a matrix in R

    6 answers



table, data.frame.



Which has 12 columns with variable names and 24 rows df



Like:



Var1 Var2 Var3 Var4 Var12
1 NA 2 3 4
5 6 2 3 3
NA 7 8 NA 4


And I want to calculate the mean for each column while ignoring the Na's
For example:



colMeans(df)



And get the result like:



Var1 Var2 Var3 Var4 Var12
3 6,5 4 3 3,66


I don't want the NA's to be considered in the calculation of Mean.



I tried some methods like na.omit, !is.na, but I don't get the desired result like what I described above.





This question already has an answer here:



  • calculate the mean for each column of a matrix in R

    6 answers







r data.table mean






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 10:59









Ronak Shah

82k13 gold badges50 silver badges87 bronze badges




82k13 gold badges50 silver badges87 bronze badges










asked Mar 28 at 10:28









Alex RikaAlex Rika

588 bronze badges




588 bronze badges





marked as duplicate by Ronak Shah r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











marked as duplicate by Ronak Shah r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Ronak Shah r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • colMeans(df, na.rm = TRUE)

    – Ronak Shah
    Mar 28 at 10:30











  • Yeah that worked ! Thanx was so easy.. :P

    – Alex Rika
    Mar 28 at 10:32











  • You might wanna post it as answer?

    – Alex Rika
    Mar 28 at 10:33











  • The calculation of Means is correct but how can I get it like horizontal while getting the col names above each mean number?

    – Alex Rika
    Mar 28 at 10:33











  • Not sure what you mean by that. Can you update the post with the expected output?

    – Ronak Shah
    Mar 28 at 10:39


















  • colMeans(df, na.rm = TRUE)

    – Ronak Shah
    Mar 28 at 10:30











  • Yeah that worked ! Thanx was so easy.. :P

    – Alex Rika
    Mar 28 at 10:32











  • You might wanna post it as answer?

    – Alex Rika
    Mar 28 at 10:33











  • The calculation of Means is correct but how can I get it like horizontal while getting the col names above each mean number?

    – Alex Rika
    Mar 28 at 10:33











  • Not sure what you mean by that. Can you update the post with the expected output?

    – Ronak Shah
    Mar 28 at 10:39

















colMeans(df, na.rm = TRUE)

– Ronak Shah
Mar 28 at 10:30





colMeans(df, na.rm = TRUE)

– Ronak Shah
Mar 28 at 10:30













Yeah that worked ! Thanx was so easy.. :P

– Alex Rika
Mar 28 at 10:32





Yeah that worked ! Thanx was so easy.. :P

– Alex Rika
Mar 28 at 10:32













You might wanna post it as answer?

– Alex Rika
Mar 28 at 10:33





You might wanna post it as answer?

– Alex Rika
Mar 28 at 10:33













The calculation of Means is correct but how can I get it like horizontal while getting the col names above each mean number?

– Alex Rika
Mar 28 at 10:33





The calculation of Means is correct but how can I get it like horizontal while getting the col names above each mean number?

– Alex Rika
Mar 28 at 10:33













Not sure what you mean by that. Can you update the post with the expected output?

– Ronak Shah
Mar 28 at 10:39






Not sure what you mean by that. Can you update the post with the expected output?

– Ronak Shah
Mar 28 at 10:39













1 Answer
1






active

oldest

votes


















1
















For a data.table dt, that looks like this:



dt
Var1 Var2 Var3 Var4 Var12
1: 1 NA 2 3 4
2: 5 6 2 3 3
3: NA 7 8 NA 4


You can simply use lapply():



dt[, lapply(.SD, mean, na.rm = TRUE)]


The result is:



 Var1 Var2 Var3 Var4 Var12
1: 3 6.5 4 3 3.666667





share|improve this answer

























  • How would you syntax the exact thing but for row means?

    – Alex Rika
    Mar 28 at 10:57











  • You could use dt[, apply(.SD, 1, mean, na.rm = TRUE)]

    – clemens
    Mar 28 at 11:02











  • for some reason now the dt[, lapply(.SD, mean, na.rm = TRUE)] gives me an error:Error in .subset(x, j) : invalid subscript type 'list' But before I didnnt get an error?

    – Alex Rika
    Mar 28 at 11:05











  • oh ok I fixed it should have converted back to data table

    – Alex Rika
    Mar 28 at 11:12














1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1
















For a data.table dt, that looks like this:



dt
Var1 Var2 Var3 Var4 Var12
1: 1 NA 2 3 4
2: 5 6 2 3 3
3: NA 7 8 NA 4


You can simply use lapply():



dt[, lapply(.SD, mean, na.rm = TRUE)]


The result is:



 Var1 Var2 Var3 Var4 Var12
1: 3 6.5 4 3 3.666667





share|improve this answer

























  • How would you syntax the exact thing but for row means?

    – Alex Rika
    Mar 28 at 10:57











  • You could use dt[, apply(.SD, 1, mean, na.rm = TRUE)]

    – clemens
    Mar 28 at 11:02











  • for some reason now the dt[, lapply(.SD, mean, na.rm = TRUE)] gives me an error:Error in .subset(x, j) : invalid subscript type 'list' But before I didnnt get an error?

    – Alex Rika
    Mar 28 at 11:05











  • oh ok I fixed it should have converted back to data table

    – Alex Rika
    Mar 28 at 11:12















1
















For a data.table dt, that looks like this:



dt
Var1 Var2 Var3 Var4 Var12
1: 1 NA 2 3 4
2: 5 6 2 3 3
3: NA 7 8 NA 4


You can simply use lapply():



dt[, lapply(.SD, mean, na.rm = TRUE)]


The result is:



 Var1 Var2 Var3 Var4 Var12
1: 3 6.5 4 3 3.666667





share|improve this answer

























  • How would you syntax the exact thing but for row means?

    – Alex Rika
    Mar 28 at 10:57











  • You could use dt[, apply(.SD, 1, mean, na.rm = TRUE)]

    – clemens
    Mar 28 at 11:02











  • for some reason now the dt[, lapply(.SD, mean, na.rm = TRUE)] gives me an error:Error in .subset(x, j) : invalid subscript type 'list' But before I didnnt get an error?

    – Alex Rika
    Mar 28 at 11:05











  • oh ok I fixed it should have converted back to data table

    – Alex Rika
    Mar 28 at 11:12













1














1










1









For a data.table dt, that looks like this:



dt
Var1 Var2 Var3 Var4 Var12
1: 1 NA 2 3 4
2: 5 6 2 3 3
3: NA 7 8 NA 4


You can simply use lapply():



dt[, lapply(.SD, mean, na.rm = TRUE)]


The result is:



 Var1 Var2 Var3 Var4 Var12
1: 3 6.5 4 3 3.666667





share|improve this answer













For a data.table dt, that looks like this:



dt
Var1 Var2 Var3 Var4 Var12
1: 1 NA 2 3 4
2: 5 6 2 3 3
3: NA 7 8 NA 4


You can simply use lapply():



dt[, lapply(.SD, mean, na.rm = TRUE)]


The result is:



 Var1 Var2 Var3 Var4 Var12
1: 3 6.5 4 3 3.666667






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 10:53









clemensclemens

4,5072 gold badges5 silver badges19 bronze badges




4,5072 gold badges5 silver badges19 bronze badges















  • How would you syntax the exact thing but for row means?

    – Alex Rika
    Mar 28 at 10:57











  • You could use dt[, apply(.SD, 1, mean, na.rm = TRUE)]

    – clemens
    Mar 28 at 11:02











  • for some reason now the dt[, lapply(.SD, mean, na.rm = TRUE)] gives me an error:Error in .subset(x, j) : invalid subscript type 'list' But before I didnnt get an error?

    – Alex Rika
    Mar 28 at 11:05











  • oh ok I fixed it should have converted back to data table

    – Alex Rika
    Mar 28 at 11:12

















  • How would you syntax the exact thing but for row means?

    – Alex Rika
    Mar 28 at 10:57











  • You could use dt[, apply(.SD, 1, mean, na.rm = TRUE)]

    – clemens
    Mar 28 at 11:02











  • for some reason now the dt[, lapply(.SD, mean, na.rm = TRUE)] gives me an error:Error in .subset(x, j) : invalid subscript type 'list' But before I didnnt get an error?

    – Alex Rika
    Mar 28 at 11:05











  • oh ok I fixed it should have converted back to data table

    – Alex Rika
    Mar 28 at 11:12
















How would you syntax the exact thing but for row means?

– Alex Rika
Mar 28 at 10:57





How would you syntax the exact thing but for row means?

– Alex Rika
Mar 28 at 10:57













You could use dt[, apply(.SD, 1, mean, na.rm = TRUE)]

– clemens
Mar 28 at 11:02





You could use dt[, apply(.SD, 1, mean, na.rm = TRUE)]

– clemens
Mar 28 at 11:02













for some reason now the dt[, lapply(.SD, mean, na.rm = TRUE)] gives me an error:Error in .subset(x, j) : invalid subscript type 'list' But before I didnnt get an error?

– Alex Rika
Mar 28 at 11:05





for some reason now the dt[, lapply(.SD, mean, na.rm = TRUE)] gives me an error:Error in .subset(x, j) : invalid subscript type 'list' But before I didnnt get an error?

– Alex Rika
Mar 28 at 11:05













oh ok I fixed it should have converted back to data table

– Alex Rika
Mar 28 at 11:12





oh ok I fixed it should have converted back to data table

– Alex Rika
Mar 28 at 11:12








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.





Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현