[英]r call column by name data.frame return data.frame
I know how to call a data.frame by column name but it returns a numeric vector rather than a data.frame. 我知道如何通过列名调用data.frame,但是它返回数字矢量而不是data.frame。 How to I force it to return a data.frame instead? 我如何强制它返回一个data.frame呢?
data = data.frame(rand1 = rnorm(5, sd = 0.3),rand2 = rnorm(5, sd = 0.3) )
data[,'rand2']
[1] 0.54355118 -0.34235808 0.30914102 -0.06509097 0.29061738
class(data[,'rand2'])
[1] "numeric"
From you example you can simply use 从您的示例中,您可以简单地使用
data = data.frame(rand1 = rnorm(5, sd = 0.3),rand2 = rnorm(5, sd = 0.3) )
data[,'rand2',drop=F]
rand2
1 -0.07895436
2 0.55478780
3 -0.47263459
4 0.17346880
5 0.47885452
class(data[,'rand2',drop=F])
[1] "data.frame"
Some other good ideas for data.frames can be found here 可以在这里找到有关data.frames的其他一些好主意
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.