R Language
डेटा फ्रेम एकत्र करना
खोज…
परिचय
एकत्रीकरण आर के लिए सबसे आम उपयोगों में से एक है। आर में ऐसा करने के कई तरीके हैं, जिन्हें हम यहां स्पष्ट करेंगे।
आधार आर के साथ एकत्रीकरण
इसके लिए, हम फंक्शन एग्रीगेट का उपयोग करेंगे, जिसका उपयोग निम्नानुसार किया जा सकता है:
aggregate(formula,function,data)
निम्न कोड कुल फ़ंक्शन का उपयोग करने के विभिन्न तरीकों को दर्शाता है।
कोड:
df = data.frame(group=c("Group 1","Group 1","Group 2","Group 2","Group 2"), subgroup = c("A","A","A","A","B"),value = c(2,2.5,1,2,1.5))
# sum, grouping by one column
aggregate(value~group, FUN=sum, data=df)
# mean, grouping by one column
aggregate(value~group, FUN=mean, data=df)
# sum, grouping by multiple columns
aggregate(value~group+subgroup,FUN=sum,data=df)
# custom function, grouping by one column
# in this example we want the sum of all values larger than 2 per group.
aggregate(value~group, FUN=function(x) sum(x[x>2]), data=df)
उत्पादन:
> df = data.frame(group=c("Group 1","Group 1","Group 2","Group 2","Group 2"), subgroup = c("A","A","A","A","B"),value = c(2,2.5,1,2,1.5))
> print(df)
group subgroup value
1 Group 1 A 2.0
2 Group 1 A 2.5
3 Group 2 A 1.0
4 Group 2 A 2.0
5 Group 2 B 1.5
>
> # sum, grouping by one column
> aggregate(value~group, FUN=sum, data=df)
group value
1 Group 1 4.5
2 Group 2 4.5
>
> # mean, grouping by one column
> aggregate(value~group, FUN=mean, data=df)
group value
1 Group 1 2.25
2 Group 2 1.50
>
> # sum, grouping by multiple columns
> aggregate(value~group+subgroup,FUN=sum,data=df)
group subgroup value
1 Group 1 A 4.5
2 Group 2 A 3.0
3 Group 2 B 1.5
>
> # custom function, grouping by one column
> # in this example we want the sum of all values larger than 2 per group.
> aggregate(value~group, FUN=function(x) sum(x[x>2]), data=df)
group value
1 Group 1 2.5
2 Group 2 0.0
सपने देखने के साथ एकत्र होना
आसान के साथ एकत्रीकरण आसान है! आप इसके लिए group_by () और संक्षेप () फ़ंक्शन का उपयोग कर सकते हैं। कुछ उदाहरण नीचे दिए गए हैं।
कोड:
# Aggregating with dplyr
library(dplyr)
df = data.frame(group=c("Group 1","Group 1","Group 2","Group 2","Group 2"), subgroup = c("A","A","A","A","B"),value = c(2,2.5,1,2,1.5))
print(df)
# sum, grouping by one column
df %>% group_by(group) %>% summarize(value = sum(value)) %>% as.data.frame()
# mean, grouping by one column
df %>% group_by(group) %>% summarize(value = mean(value)) %>% as.data.frame()
# sum, grouping by multiple columns
df %>% group_by(group,subgroup) %>% summarize(value = sum(value)) %>% as.data.frame()
# custom function, grouping by one column
# in this example we want the sum of all values larger than 2 per group.
df %>% group_by(group) %>% summarize(value = sum(value[value>2])) %>% as.data.frame()
उत्पादन:
> library(dplyr)
>
> df = data.frame(group=c("Group 1","Group 1","Group 2","Group 2","Group 2"), subgroup = c("A","A","A","A","B"),value = c(2,2.5,1,2,1.5))
> print(df)
group subgroup value
1 Group 1 A 2.0
2 Group 1 A 2.5
3 Group 2 A 1.0
4 Group 2 A 2.0
5 Group 2 B 1.5
>
> # sum, grouping by one column
> df %>% group_by(group) %>% summarize(value = sum(value)) %>% as.data.frame()
group value
1 Group 1 4.5
2 Group 2 4.5
>
> # mean, grouping by one column
> df %>% group_by(group) %>% summarize(value = mean(value)) %>% as.data.frame()
group value
1 Group 1 2.25
2 Group 2 1.50
>
> # sum, grouping by multiple columns
> df %>% group_by(group,subgroup) %>% summarize(value = sum(value)) %>% as.data.frame()
group subgroup value
1 Group 1 A 4.5
2 Group 2 A 3.0
3 Group 2 B 1.5
>
> # custom function, grouping by one column
> # in this example we want the sum of all values larger than 2 per group.
> df %>% group_by(group) %>% summarize(value = sum(value[value>2])) %>% as.data.frame()
group value
1 Group 1 2.5
2 Group 2 0.0
Data.table के साथ एकत्रीकरण
Data.table पैकेज के साथ समूहीकरण वाक्यविन्यास dt[i, j, by]
का उपयोग करके किया जाता है dt[i, j, by]
जोर से पढ़ा जा सकता है: " dt का उपयोग करें, i का उपयोग करते हुए पंक्तियाँ लें, फिर j, समूहीकृत करके समूह की गणना करें। " dt कथन के भीतर । , कई गणना या समूहों को एक सूची में रखा जाना चाहिए। चूंकि list()
लिए एक उपनाम list()
है .()
, दोनों का उपयोग परस्पर विनिमय किया जा सकता है। नीचे दिए गए उदाहरणों में हम उपयोग करते हैं .()
।
कोड:
# Aggregating with data.table
library(data.table)
dt = data.table(group=c("Group 1","Group 1","Group 2","Group 2","Group 2"), subgroup = c("A","A","A","A","B"),value = c(2,2.5,1,2,1.5))
print(dt)
# sum, grouping by one column
dt[,.(value=sum(value)),group]
# mean, grouping by one column
dt[,.(value=mean(value)),group]
# sum, grouping by multiple columns
dt[,.(value=sum(value)),.(group,subgroup)]
# custom function, grouping by one column
# in this example we want the sum of all values larger than 2 per group.
dt[,.(value=sum(value[value>2])),group]
उत्पादन:
> # Aggregating with data.table
> library(data.table)
>
> dt = data.table(group=c("Group 1","Group 1","Group 2","Group 2","Group 2"), subgroup = c("A","A","A","A","B"),value = c(2,2.5,1,2,1.5))
> print(dt)
group subgroup value
1: Group 1 A 2.0
2: Group 1 A 2.5
3: Group 2 A 1.0
4: Group 2 A 2.0
5: Group 2 B 1.5
>
> # sum, grouping by one column
> dt[,.(value=sum(value)),group]
group value
1: Group 1 4.5
2: Group 2 4.5
>
> # mean, grouping by one column
> dt[,.(value=mean(value)),group]
group value
1: Group 1 2.25
2: Group 2 1.50
>
> # sum, grouping by multiple columns
> dt[,.(value=sum(value)),.(group,subgroup)]
group subgroup value
1: Group 1 A 4.5
2: Group 2 A 3.0
3: Group 2 B 1.5
>
> # custom function, grouping by one column
> # in this example we want the sum of all values larger than 2 per group.
> dt[,.(value=sum(value[value>2])),group]
group value
1: Group 1 2.5
2: Group 2 0.0