proportion table with cut()?

I'm trying to make a table that shows the proportion of events (with variable 'duration') that last longer than an hour vs shorter than an hour (according to variable 'hours'). This code is not working, but I don't understand how to write it correctly. Any help is much appreciated!!

table <- prop.table(trips$duration, cut(hours, breaks = c(min(hours), 1, max(hours))))

I do not think you want to use the prop.table() function but I am not sure I understand the structure of your data. Can you post a small data sample? The result of

dput(head(trips, 10))

might be enough.

It seems that duration is a time in seconds and hours is that same time expressed in hours. You can make a table showing the proportion of values that are less than one hour using this code.

table(trips$hours < 1)/nrow(trips)

For the data sample you posted, that shows that FALSE has a proportion of 0.1 because only one of the ten values is greater than one.

You can print the table with

table_12

Does that not meet your needs?

Not really... I can see the printed table, but I don't understand why, in environment, it is shown as a value, rather than data. A previous table I created with the code below is shown as data. I can double-click it to view.
table_9 <- with(trips, tapply(hours, list(gender=gender,agecat=agecat), mean))

I've come to the following line of code:
hours_cat <- with(trips, {
cut(hours,
breaks = c(min(hours), 1, max(hours)),
include.lowest=TRUE)
})
table_12 <- table(hours_cat)/nrow(trips)

which works perfectly:

table_12 hours_cat [0,1] (1,56.9] 0.99392476 0.00607524

But the same problem remains: It is shown as a value in environment, and double-clicking this doesn't show the table.

The distinction between Data and Values in the Environment pane is somewhat arbitrary. I do not know all of the details but one dimensional objects (vectors) appear as values and objects with more dimensions, such as data frames or matrices, appear as data. The table generated by my code is one dimensional, so it gets called a Value.
You can view the content of the table by running

View(table_12)

though it is presented rotated 90 degrees from how it is printed.

The distinction between data and values disappears if you switch the Environment pane to show a Grid. Near the upper right corner of the Environment pane you should see the word List. Click the small arrow next to that and select Grid to see a different display of the objects.

That makes a lot of sense! I'm still in the early stages of figuring out this language, so I'm not always knowledgeable about what goes on 'under the hood'. Many thanks for your help!!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thanks for the quick reply!!
dput(head(trips, 10))
structure(list(id = c(119L, 174L, 218L, 371L, 448L, 494L, 654L,
788L, 836L, 849L), duration = c(394L, 386L, 589L, 3715L, 723L,
274L, 293L, 877L, 708L, 376L), start_station = c(10L, 24L, 22L,
47L, 31L, 39L, 32L, 27L, 38L, 10L), end_station = c(32L, 48L,
49L, 47L, 35L, 39L, 14L, 16L, 22L, 32L), birth_date = c(1975L,
1986L, 1978L, 1973L, 1978L, 1974L, 1981L, 1958L, 1962L, 1974L
), gender = c("Male", "Male", "Male", "Female", "Male", "Male",
"Male", "Male", "Female", "Male"), subscription_type = c("Registered",
"Registered", "Registered", "Registered", "Registered", "Registered",
"Registered", "Registered", "Registered", "Registered"), hours = c(0.109444444444444,
0.107222222222222, 0.163611111111111, 1.03194444444444, 0.200833333333333,
0.0761111111111111, 0.0813888888888889, 0.243611111111111, 0.196666666666667,
0.104444444444444), age = c(36, 25, 33, 38, 33, 37, 30, 53, 49,
37), agecat = structure(c(2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("junior", "adult", "senior"), class = "factor")), row.names = c(1L,
2L, 3L, 8L, 12L, 14L, 18L, 22L, 25L, 26L), class = "data.frame")

Thanks! That definitely seems to work.
However, labels are 'TRUE' and 'FALSE'. I should be able to rename these in 'duration >1' and 'duration <1' , right?

Also:
table_12 <- table(trips$hours < 1)/nrow(trips)
creates a value (in environment), not a table :S

You can assign names with the names function. Having spaces in names is not a good idea but it can be done.

TBL <- table(trips$hours < 1)/nrow(trips)
names(TBL) <- c("dur. > 1", "dur. < 1")

Doesn't seem to work...
table_12 <- table(trips$hours < 1)/nrow(trips)
creates a value (in environment), not a table :S