Home > Net >  Adding a percentage density column to a r dataframe
Adding a percentage density column to a r dataframe

Time:01-30

I want to add a percentage density column next to the frequency column in the as dataframe below. As well as the sum values for the frequency and percentage density columns. The percentage density column shows the weigh of the percentage for each sequence so if there are 10 sequences in total and the frequency for that one sequence is 3 then the percentage density would be 3/10 = 0.3 . The sum of the percentage density should be 1.0.

data <- c(3.968, 3.534, 4.032, 3.912, 3.572, 4.014, 3.682, 3.608, 3.669, 3.705,
4.023, 3.588, 3.945, 3.871, 3.744, 3.711, 3.645, 3.977, 3.888, 3.948)
sortdata <- sort(data)
as.data.frame(table(cut(sortdata,breaks=seq(3.50,4.15,by=0.05))))

CodePudding user response:

Try the prop.table() function. You can wrap it around a table and then combine with your frequency counts as such:

# Proportion Of The Total For Each Cut
prop.table(table(cut(sortdata,breaks=seq(3.50,4.15,by=0.05))))

# Data Frame With Frequencies And Proportions Combined
data.df <- as.data.frame(
    cbind(
        table(cut(sortdata,breaks=seq(3.50,4.15,by=0.05))),
        prop.table(table(cut(sortdata,breaks=seq(3.50,4.15,by=0.05))))
        )
    )

names(data.df) <- c("Freq","Pct")

data.df

# Check Sum Of Pct Equals 1
sum(data.df$Pct) == 1

CodePudding user response:

Piping Freq into proportions.

res <- as.data.frame(table(cut(sortdata, breaks=seq(3.50, 4.15, by=0.05)))) |>
  transform(Dens=proportions(Freq))
res
#          Var1 Freq Dens
# 1  (3.5,3.55]    1 0.05
# 2  (3.55,3.6]    2 0.10
# 3  (3.6,3.65]    2 0.10
# 4  (3.65,3.7]    2 0.10
# 5  (3.7,3.75]    3 0.15
# 6  (3.75,3.8]    0 0.00
# 7  (3.8,3.85]    0 0.00
# 8  (3.85,3.9]    2 0.10
# 9  (3.9,3.95]    3 0.15
# 10   (3.95,4]    2 0.10
# 11   (4,4.05]    3 0.15
# 12 (4.05,4.1]    0 0.00
# 13 (4.1,4.15]    0 0.00

## check
res$Dens |> sum()
# [1] 1
  • Related