How Does the 'Prop.Table()' Function Work in R

How does the `prop.table()` function work in r?

The values in each cell divided by the sum of the 4 cells:

prop.table(m)

The value of each cell divided by the sum of the row cells:

prop.table(m, 1)

The value of each cell divided by the sum of the column cells:

prop.table(m, 2)

General question about prop.table function in R

No, there's no way to edit the prop.table function to give you your desired result. If you want the sum of your percentages to equal %100, then take a subset of the Cohort "A" first and then tabulate.

with(subset(dataframe, Cohort=="A"),
round(100*prop.table(table(Nstage)), 2)
)

How do I get a prop.table in data.table?

"We don't need no steenking counts." # classic movie reference

I think you must have some sort of conceptual barrier to understanding the "true power" of the second argument position of the [.data.table function. You just put an expression to be evaluated. Since prop.table expects a table object, you must first construct one with the table function:

mydata2[ , prop.table(table(Improved)) ]
Improved
None Some Marked
0.5000000 0.1666667 0.3333333

I do note that the returned value is not a data.table object.

Is there a way to round the results of the prop.table function in R?

Its all about the paranthesis:

with(mtcars, table(vs, am) |> prop.table(margin = 1) * 100) |>round(1)

am
vs 0 1
0 66.7 33.3
1 50.0 50.0

Note that what you have does not round the results of prop.table but rather rounds 100. If you want you could do

(with(mtcars, table(vs, am)) |> prop.table(margin = 1) * 100)|>round(1)

  am
vs 0 1
0 66.7 33.3
1 50.0 50.0

s3 is there a way to combine prop.table for character variables?

Maybe this is what you wanted. Values in each category sum up to 100%.

lis <- sapply( Student1995, function(x) t( sapply( x, table ) ) )

sapply( lis, function(x) colSums(prop.table(x)) )
$alcohol
Not Once.or.Twice.a.week Once.a.month
0.0 0.6 0.4
Once.a.week More.than.once.a.week
0.0 0.0

$drugs
Not Tried.once Occasional Regular
0.8 0.2 0.0 0.0

$smoke
Not Occasional Regular
0.6 0.2 0.2

$sport
Not.regular Regular
0.4 0.6

and the whole summary...

prop.table( table(as.vector( sapply( Student1995, unlist ))) )

Not Not regular Occasional
0.35 0.10 0.05
Once a month Once or Twice a week Regular
0.10 0.15 0.20
Tried once
0.05

How can I apply `prop.table` on the margin of a table in r?

You can sum rows then calculate the table :

prop.table(rowSums(mymatrix3,2))

> Northeast North Central South West
0.166 0.242 0.386 0.206

Error: could not find function ... in R

There are a few things you should check :

  1. Did you write the name of your function correctly? Names are case sensitive.
  2. Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once)
  3. Did you attach that package to the workspace ?
    require(thePackage) (and check its return value) or library(thePackage) (this should be done every time you start a new R session)
  4. Are you using an older R version where this function didn't exist yet?
  5. Are you using a different version of the specific package? This could be in either direction: functions are added and removed over time, and it's possible the code you're referencing is expecting a newer or older version of the package than what you have installed.

If you're not sure in which package that function is situated, you can do a few things.

  1. If you're sure you installed and attached/loaded the right package, type help.search("some.function") or ??some.function to get an information box that can tell you in which package it is contained.
  2. find and getAnywhere can also be used to locate functions.
  3. If you have no clue about the package, you can use findFn in the sos package as explained in this answer.
  4. RSiteSearch("some.function") or searching with rdocumentation or rseek are alternative ways to find the function.

Sometimes you need to use an older version of R, but run code created for a newer version. Newly added functions (eg hasName in R 3.4.0) won't be found then. If you use an older R version and want to use a newer function, you can use the package backports to make such functions available. You also find a list of functions that need to be backported on the git repo of backports. Keep in mind that R versions older than R3.0.0 are incompatible with packages built for R3.0.0 and later versions.



Related Topics



Leave a reply



Submit