[R] dataframe manipulation

kestickler@netscape.net kestickler at netscape.net
Mon Oct 1 03:22:36 CEST 2001


Hi,

a couple of questions on manipulating a dataframe:

1) Without using a for loop (often inefficient, as i understand, in R), is it possible to split a dataframe into a list of dataframes with the number of *columns* in each (child)dataframe specified by the elements of a numeric vector?
for example:

dataframe
10 20 30 40
24 90 34 12
12 44 67 77
split using the vector c(3, 1)
results in 2 dataframes of 3 and 1 column.
10 20 30
24 90 34
12 44 67

and
40
12
77


2) To find the abs() of the difference between every element of a dataframe and the element in the nth column in the *same row*, i use the following code:

n <- 4
apply(df, 1, myDist, df[n,])
myDist <- function(x, y) {
    dist <- sum(abs(x-y));
    return (dist);
}


This however, produces an error message:
Fails as: Error in abs: non-numeric argument to function
I guess its because df[n,] creates a dataframe and not a vector. Any suggestions on how to modify the function?


Thanks for your help,

ken




__________________________________________________________________




-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list