Newbie struggling in rowwise percent calculation of two columns

Simple example

dat1 <- data.frame(
  stringsAsFactors = FALSE,
                id = c("A", "B", "C"),
            budget = c(1066500L, 100464L, 77618L),
    personal_costs = c(658168L, 254310L, 67826L)
        )

dat1$percent <-  dat1$personal_costs / sum(dat1$budget) * 100

There are other ways to do this.

For a general outline for asking questions

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

1 Like