I have a dataframe in R with 3 columns (variables). One of them, called Region, is some kind nested. I try to reproduce a little part of it.
df <- data.frame (freq = c(70, 72, 74, 76, 78,
70, 72, 74, 76, 78,
70, 72, 74, 76, 78),
region = c('region.1','region.1','region.1','region.1', 'region.1',
'region.1.1','region.1.1','region.1.1', 'region.1.1', 'region.1.1',
'region.2','region.2', 'region.2', 'region.2', 'region.2'),
dBvalue = c(-30, -32, -42, -45, -47,
-33, -28, -22, -37, -35,
-36, -55, -43, -26, -49))
Now I want to add 3 new colums. The first with the count of the observations per region (so in this case will be 1...5, 1...5, etc), the second must contain a grouping value, ant the last should have the higher hierarchical level of aggregation of the Region column in this case the final df would be:
df <- data.frame (freq = c(70, 72, 74, 76, 78,
70, 72, 74, 76, 78,
70, 72, 74, 76, 78),
region = c('region.1','region.1','region.1','region.1', 'region.1',
'region.1.1','region.1.1','region.1.1', 'region.1.1', 'region.1.1',
'region.2','region.2', 'region.2', 'region.2', 'region.2'),
dBvalue = c(-30, -32, -42, -45, -47,
-33, -28, -22, -37, -35,
-36, -55, -43, -26, -49),
count = c(1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5),
group = c(1,1,1,1,1,
2,2,2,2,2,
3,3,3,3,3),
higher_region = c("region.1","region.1","region.1","region.1","region.1",
"region.1","region.1","region.1","region.1","region.1",
"region.2","region.2","region.2","region.2","region.2"))
I'm trying with loop functions but i'm going crazy. Someone has a solution? Maybe using alternative methods?
In Base you can use match
with unique
to find the group , ave
with seq_along
to get the count and strsplit
with paste
to get the higher_region .
df$group <- match(df$region, unique(df$region))
#df$group <- unclass(factor(df$region)) #Alternative
df$count <- ave(df$group, df$region, FUN=seq_along)
df$higher_region <- sapply(strsplit(df$region, ".", TRUE),
function(x) paste(x[1:2], collapse = "."))
#df$higher_region <- sub("^([^.]+\\.[^.]*).*", "\\1", df$region) #Alternative
df
# freq region dBvalue count group higher_region
#1 70 region.1 -30 1 1 region.1
#2 72 region.1 -32 2 1 region.1
#3 74 region.1 -42 3 1 region.1
#4 76 region.1 -45 4 1 region.1
#5 78 region.1 -47 5 1 region.1
#6 70 region.1.1 -33 1 2 region.1
#7 72 region.1.1 -28 2 2 region.1
#8 74 region.1.1 -22 3 2 region.1
#9 76 region.1.1 -37 4 2 region.1
#10 78 region.1.1 -35 5 2 region.1
#11 70 region.2 -36 1 3 region.2
#12 72 region.2 -55 2 3 region.2
#13 74 region.2 -43 3 3 region.2
#14 76 region.2 -26 4 3 region.2
#15 78 region.2 -49 5 3 region.2
Suggestion using 'dplyr' for the grouping and counting, as well as 'as.factor' to 'recode' the regions into "he second must contain a grouping value" (although this encodes the same information as 'region', maybe I misunderstood your specification. Finally we use 'gsub' to extract the first digit from 'region'
df <- data.frame (freq = c(70, 72, 74, 76, 78,
70, 72, 74, 76, 78,
70, 72, 74, 76, 78),
region = c('region.1','region.1','region.1','region.1', 'region.1',
'region.1.1','region.1.1','region.1.1', 'region.1.1', 'region.1.1',
'region.2','region.2', 'region.2', 'region.2', 'region.2'),
dBvalue = c(-30, -32, -42, -45, -47,
-33, -28, -22, -37, -35,
-36, -55, -43, -26, -49))
library(dplyr)
df %>%
group_by(region) %>%
mutate(count = row_number()) %>%
ungroup() %>%
mutate(group = as.numeric(as.factor(region)),
higher_region = gsub("[a-z.]*([0-9]).*", "\\1", region)) %>%
as.data.frame()
Returns:
freq region dBvalue count group higher_region 1 70 region.1 -30 1 1 1 2 72 region.1 -32 2 1 1 3 74 region.1 -42 3 1 1 4 76 region.1 -45 4 1 1 5 78 region.1 -47 5 1 1 6 70 region.1.1 -33 1 2 1 7 72 region.1.1 -28 2 2 1 8 74 region.1.1 -22 3 2 1 9 76 region.1.1 -37 4 2 1 10 78 region.1.1 -35 5 2 1 11 70 region.2 -36 1 3 2 12 72 region.2 -55 2 3 2 13 74 region.2 -43 3 3 2 14 76 region.2 -26 4 3 2 15 78 region.2 -49 5 3 2
Account for two digit 'higher regions':
library(dplyr)
df %>%
group_by(region) %>%
mutate(count = row_number()) %>%
ungroup() %>%
mutate(group = as.numeric(as.factor(region)),
higher_region = gsub("(region.[0-9]+)\\.?.*", "\\1", region)) %>%
as.data.frame()
freq region dBvalue count group higher_region
1 70 region.1 -30 1 1 region.1
2 72 region.1 -32 2 1 region.1
3 74 region.1 -42 3 1 region.1
4 76 region.1 -45 4 1 region.1
5 78 region.1 -47 5 1 region.1
6 70 region.1.1 -33 1 2 region.1
7 72 region.1.1 -28 2 2 region.1
8 74 region.1.1 -22 3 2 region.1
9 76 region.1.1 -37 4 2 region.1
10 78 region.1.1 -35 5 2 region.1
11 70 region.2 -36 1 3 region.2
12 72 region.2 -55 2 3 region.2
13 74 region.2 -43 3 3 region.2
14 76 region.2 -26 4 3 region.2
15 78 region.2 -49 5 3 region.2
additional option. dplyr 1.0.0
library(tidyverse)
df %>%
group_by(region) %>%
mutate(count = row_number(),
group = cur_group_id(),
higher_region = str_extract(region, "^[A-z]*\\.\\d+")) %>%
ungroup()
Base R solution:
within(df, {
region_str <- as.character(region)
higher_region <- ifelse(grepl("[.]\\d[.]", region_str),
gsub("[.]\\d$", "", region_str), region_str)
count <- ave(region_str, region_str, FUN = length)
rm(region_str)
}
)
Base R solution:
df$count <- unlist(lapply(split(df,df$region), function(x) 1:nrow(x)))
df$group <- unlist(lapply(1:length(unique(df$region)), function(x) rep(x, nrow(split(df,df$region)[[x]]))))
df$higher_region <- sub("(region\\.\\d+).*","\\1",df$region)
> df
freq region dBvalue count group higher_region
1 70 region.1 -30 1 1 region.1
2 72 region.1 -32 2 1 region.1
3 74 region.1 -42 3 1 region.1
4 76 region.1 -45 4 1 region.1
5 78 region.1 -47 5 1 region.1
6 70 region.1.1 -33 1 2 region.1
7 72 region.1.1 -28 2 2 region.1
8 74 region.1.1 -22 3 2 region.1
9 76 region.1.1 -37 4 2 region.1
10 78 region.1.1 -35 5 2 region.1
11 70 region.2 -36 1 3 region.2
12 72 region.2 -55 2 3 region.2
13 74 region.2 -43 3 3 region.2
14 76 region.2 -26 4 3 region.2
15 78 region.2 -49 5 3 region.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.