改变“名字”列表的属性
您好,我正在分析一些数据并尝试使用包含必须以这种方式格式化的对象的包。我从未见过这种类型的格式,也不知道如何生成它。当我在 R 中调用该对象时,这是一个奇怪的对象:
> a
A B C D E F
0.00000000 1.34529412 0.31571429 1.26327103 0.32615385 1.12585586
这是该对象 a 的一些属性:
> str(a)
Named num [1:6] 0 1.345 0.316 1.263 0.326 ...
- attr(*, "names")= chr [1:6] "" "A" "B" "C" ...
> class(a)
[1] "numeric"
我可以使用标准 write.csv 命令将“a”写入 .csv 格式,并且它可以用两列很好地格式化它,一列包含字符,另一列包含数字。当我尝试使用 read.csv 在 R 中读回它时,它会正确地将其返回为具有两列的数据框。但是,我尝试使用的函数不喜欢数据帧格式,并且更喜欢“a”所在的任何格式。
因此可以采用一个示例数据集,例如:
> L <- c("A","B","C","D","E","F")
> R <- c(0.00000000,1.34529412,0.31571429,1.26327103,0.32615385,1.12585586 )
> T <- list(L=L,R=R)
> Example <- as.data.frame(T)
> Example
L R
1 A 0.00000000
2 B 1.34529412
3 C 0.31571429
4 D 1.26327103
5 E 0.32615385
6 F 1.12585586
并将其转回此?
A B C D E F
0.00000000 1.34529412 0.31571429 1.26327103 0.32615385 1.12585586
有这些属性吗?
> str(a)
Named num [1:6] 0 1.345 0.316 1.263 0.326 ...
- attr(*, "names")= chr [1:6] "" "A" "B" "C" ...
> class(a)
[1] "numeric"
谢谢您的帮助!
Hello I am analyzing some data and trying to use a package that contains a object that has to be formatted in such a way. I have never seen this type of format and I am not sure how to generate it. When I call the object in R this is the strange object:
> a
A B C D E F
0.00000000 1.34529412 0.31571429 1.26327103 0.32615385 1.12585586
Here are some attributes of this object a:
> str(a)
Named num [1:6] 0 1.345 0.316 1.263 0.326 ...
- attr(*, "names")= chr [1:6] "" "A" "B" "C" ...
> class(a)
[1] "numeric"
I can write "a" to a .csv format with the standard write.csv command, and it formats it nicely with two columns, one column with the characters, and the other with the numbers. When I try to read it back in R using read.csv it returns it correctly as a data frame with two columns. However, the function that I am trying to use doesn't like the data frame format and prefers whatever format "a" is in.
So it is possible to take an example data set such as:
> L <- c("A","B","C","D","E","F")
> R <- c(0.00000000,1.34529412,0.31571429,1.26327103,0.32615385,1.12585586 )
> T <- list(L=L,R=R)
> Example <- as.data.frame(T)
> Example
L R
1 A 0.00000000
2 B 1.34529412
3 C 0.31571429
4 D 1.26327103
5 E 0.32615385
6 F 1.12585586
And turn it back into this?
A B C D E F
0.00000000 1.34529412 0.31571429 1.26327103 0.32615385 1.12585586
With these attributes?
> str(a)
Named num [1:6] 0 1.345 0.316 1.263 0.326 ...
- attr(*, "names")= chr [1:6] "" "A" "B" "C" ...
> class(a)
[1] "numeric"
Thank you for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这就是您正在寻找的东西:
I think this is the sort of thing you're looking for:
您应该学习使用 save(),而不是使用 write.table() 或 write.csv() 写入磁盘。然后您可以放心,当您重新加载()对象时,您将获得正确构造的副本。如果需要以 ASCII 可读格式查看对象,可以使用 dump()。
Instead of writing to disk with write.table() or write.csv(), you should learn to use save(). You can then be assured that when you load() the object back in, you will get a properly constructed copy. If you need to look at the object in an ASCII-readable format you can use dump().
数字 [1, 1:6] 0 1.345 0.316 1.263 0.326 ...
- attr(*, "名称")= chr [1:6] "A" "B" "C" "D" ...
num [1, 1:6] 0 1.345 0.316 1.263 0.326 ...
- attr(*, "names")= chr [1:6] "A" "B" "C" "D" ...