tags:

views:

113

answers:

2

I have a data frame with several columns. rows have names.

I want to calculate some value for each row (col1/col2) and create a new data frame with the original row names. If I just do something like data$col1/data$col2 I get a vector with the results but lose the row names.

i know it's very basic but I'm quite new to R.

+2  A: 

use the function names() to add the names :

Data <- data.frame(col1=rnorm(10),col2=rnorm(10),row.names=letters[1:10])
x <- Data$col1/Data$col2
names(x) <- row.names(Data)

This solution gives a vector with the names. To get a data-frame (solution from Marek) :

NewFrame <- data.frame(x=Data$col1/Data$col2,row.names=row.names(Data))
Joris Meys
You missed `data.frame` keyword.
Marek
@Marek : thx. I need more coffee...
Joris Meys
+4  A: 

It would help to read ?"[.data.frame" to understand what's going on. Specifically:

Note that there is no ‘data.frame’ method for ‘$’, so ‘x$name’ uses the default method which treats ‘x’ as a list.

You will see that the object's names are lost if you convert a data.frame to a list (using Joris' example data):

> as.list(Data)
$col1
 [1] -0.2179939 -2.6050843  1.6980104 -0.9712305  1.6953474  0.4422874
 [7] -0.5012775  0.2073210  1.0453705 -0.2883248

$col2
 [1] -1.3623349  0.4535634  0.3502413 -0.1521901 -0.1032828 -0.9296857
 [7]  1.4608866  1.1377755  0.2424622 -0.7814709

My suggestion would be to avoid using $ if you want to keep row names. Use this instead:

> Data["col1"]/Data["col2"]
         col1
a   0.1600149
b  -5.7435947
c   4.8481157
d   6.3816918
e -16.4146120
f  -0.4757387
g  -0.3431324
h   0.1822161
i   4.3114785
j   0.3689514
Joshua Ulrich
Josh, what's the advantage of ["name"] instead of $name? I tend to use the latter and I see you prefer the prior. My preference comes only from habit, not conscious decision.
JD Long
@JD I actually don't prefer the former. I use `$` and `$<-` often, but I don't care about row names... which is the advantage of using `[.data.frame` (it doesn't drop row names).
Joshua Ulrich
oh duh, you said 'row names' in your answer but somehow it didn't register in my head. Yep, I see that now. Sorry for being so dense ;)
JD Long