开发者

basic R question on manipulating dataframes

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

I want to calcu开发者_如何学Pythonlate 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.


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


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))


A very simple and neat way is to use row.names(data frame) to store it as a column and further manipulate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜