Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class "By" into dataframe in R

Tags:

dataframe

r

I'm using by() to evaluate a function by factors in my dataframe, but I need to use the results in a table form.

I've seen a use of as.data.frame.table to get a "By" class object into a data frame, but I'm not sure if this only works when the number of factors employed in the by() function is the same as the length of the "by" output. Using as.data.frame.table I get the following error

"...arguments imply differing number of rows: 10, 33"

Is there another way of doing this? Can tapply be used instead of by() to get a different output class?

btw, I'm using by() to convert my data into a frequency table and then regroup by standard bins

BT_by <- by(BT_H, BT_H$Tax_pp, function(BT_H) hist(rep.int(BT_H$Altitude, BT_H$Count), breaks = seq(0,6600,200), plot = FALSE)$counts)

Any help would be appreciated.

like image 850
CCID Avatar asked Apr 02 '10 02:04

CCID


People also ask

How do you turn something into a Dataframe in R?

as. data. frame() function in R Programming Language is used to convert an object to data frame.

Which function coerces an existing object to a data frame if possible?

as_tibble() turns an existing object, such as a data frame or matrix, into a so-called tibble, a data frame with class tbl_df . This is in contrast with tibble() , which builds a tibble from individual columns. as_tibble() is to tibble() as base::as.

What is a class in a Dataframe?

The DataFrame class extends the DataTable virtual class and supports the storage of any type of object (with length and [ methods) as columns.


1 Answers

The output of by is essentially just a list. If you want to combine those vectors, you can use do.call(rbind, BT_by) (or cbind depending on what shape you actually want).

like image 189
Jonathan Chang Avatar answered Oct 08 '22 18:10

Jonathan Chang