Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to batch calculate the average values of different factors and levels in R language summary statistics

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to batch calculate the averages of different factors and different levels in R language summary statistics. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In practice, we need to calculate the average value of the data. Here I compare the methods of aggregate and data.table. The tests mainly include:

1, calculate the average of the data yield

2. Calculate the average value of different levels of N

3. Calculate the average values of different levels of N and P

1. Conventional method aggregate

Code:

Data (npk)

Head (npk)

Aggregate (yield~N,data=npk,FUN = mean)

Aggregate (yield~N+P,data=npk,FUN = mean)

Result

> aggregate (yield~N,data=npk,FUN = mean)

N yield

1 0 52.06667

2 1 57.68333

> aggregate (yield~N+P,data=npk,FUN = mean)

N P yield

1 0 0 51.71667

21 0 59.21667

3 0 1 52.41667

4 1 1 56.15000

two。 Use the data.table method

Code:

Data (npk)

Head (npk)

Library (data.table)

SetDT (npk)

# single variable

Npk [, mean (yield), by=N]

# two variables

Npk [, mean (yield), by=c ("N", "P")]

# another way to write two variables

Npk [, mean (yield), by=list (NMagol P)]

Npk [, mean (yield), by=. (NMagol P)]

Results:

> # single variable

> npk [, mean (yield), by=N]

N V1

1: 0 52.06667

2: 1 57.68333

>

> # two variables

> npk [, mean (yield), by=c ("N", "P")]

N P V1

1: 0 1 52.41667

2: 1 1 56.15000

3: 0 0 51.71667

4: 1 0 59.21667

>

>

> # another way to write two variables

> npk [, mean (yield), by=list (NMague P)]

N P V1

1: 0 1 52.41667

2: 1 1 56.15000

3: 0 0 51.71667

4: 1 0 59.21667

> npk [, mean (yield), by=. (NMague P)]

N P V1

1: 0 1 52.41667

2: 1 1 56.15000

3: 0 0 51.71667

4: 10 59.21667 this is the end of the article on "how to batch calculate the average of different factors and different levels in R language Summary Statistics". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report