Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a data frame from-to R and C using .call()

Tags:

c

r

Is there a general way of passing a data frame with arbitrary columns (integer/factor, numeric, character data) from r to c and back? Pointers to close enough examples would be greatly appreciated.

Thanks.

RT

like image 662
user151410 Avatar asked Jul 12 '11 00:07

user151410


People also ask

How do you call the column of a DataFrame in a function in R?

To access a specific column in a dataframe by name, you use the $ operator in the form df$name where df is the name of the dataframe, and name is the name of the column you are interested in. This operation will then return the column you want as a vector.

How do I apply a DataFrame to a function in R?

In R Programming Language to apply a function to every integer type value in a data frame, we can use lapply function from dplyr package. And if the datatype of values is string then we can use paste() with lapply.

What is as data frame () in R?

A data frame is the most common way of storing data in R and, generally, is the data structure most often used for data analyses. Under the hood, a data frame is a list of equal-length vectors. Each element of the list can be thought of as a column and the length of each element of the list is the number of rows.

How do you access data from a DataFrame in R?

The column items in a data frame in R can be accessed using: Single brackets [] , which would display them as a column. Double brackets [[]] , which would display them as a list. Dollar symbol $ , which would display them as a list.


1 Answers

data.frame type is a list with "data.frame" attribute.

This is example of creating data.frame in source of R (src/library/stats/src/model.c):

/* Turn the data "list" into a "data.frame" */
/* so that subsetting methods will work. */

PROTECT(tmp = mkString("data.frame"));
setAttrib(data, R_ClassSymbol, tmp);
UNPROTECT(1);

You can simulate data.frame manually this way:

l <- list(1:5)
attr(l, "class") <- "data.frame"
attr(l, "names") <- "Column 1"
attr(l, "row.names") <- paste("Row ", 1:5)
like image 132
17 revs, 13 users 59% Avatar answered Oct 15 '22 10:10

17 revs, 13 users 59%