楼主: jxapp_6451
4633 9

[问答] Excel数据导入R,如何导入成列表形式 [推广有奖]

  • 0关注
  • 0粉丝

小学生

92%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
310 点
帖子
11
精华
0
在线时间
9 小时
注册时间
2016-11-7
最后登录
2016-12-14

楼主
jxapp_6451 发表于 2016-11-7 22:23:08 |AI写论文
20论坛币
RT,本人需要用下面这个函数录入数据(Polysat包中的函数)
Genotypes(mygen, loci="lG") <- list(c(120, 124, 130, 136), c(122, 120),c(128, 130, 134))

因为数据都存储在excel中,首先需要将excel中的数据导入到R中,但read.table函数默认将数据存储为数据框的形式,而且这个函数不识别NA(数据里有NA会报错),请问各位大神,有没有什么方法能把数据转化为合适的格式?


最佳答案

zhangyangsmith 查看完整内容

I would not worry about the attributes. It would not hurt and you will be able to use the elements of the list the same way as a vector without any attributes. If you insist on non-attribute vectors in the list:In your loop you will have to replacebyto make it work.
关键词:excel数据 EXCEL 数据导入 xcel exce excel Excel

回帖推荐

zhangyangsmith 发表于2楼  查看完整内容

I would not worry about the attributes. It would not hurt and you will be able to use the elements of the list the same way as a vector without any attributes. If you insist on non-attribute vectors in the list:In your loop you will have to replacebyto make it work.

沙发
zhangyangsmith 发表于 2016-11-7 22:23:09
jxapp_6451 发表于 2016-11-10 02:43
列表里会多出信息,
试了一下只有这样能返回需要的结果
然后尝试了这个循环无果
I would not worry about the attributes. It would not hurt and you will be able to use the elements of the list the same way as a vector without any attributes.

If you insist on non-attribute vectors in the list:
  1. lapply( list(a, b, c), function(v) v[!is.na(v)] )
复制代码
In your loop you will have to replace
  1. D[i]=D[i][!is.na(D[i])]
复制代码
by
  1. D[i]<<-D[i][!is.na(D[i])]
复制代码
to make it work.

藤椅
johnmy 发表于 2016-11-8 02:26:24
可以把数据框转为list啊

板凳
jxapp_6451 发表于 2016-11-8 13:01:16
johnmy 发表于 2016-11-8 02:26
可以把数据框转为list啊
那具体怎么操作?我还是小白刚接触R,,

报纸
johnmy 发表于 2016-11-9 04:14:41
as.list

地板
jxapp_6451 发表于 2016-11-9 21:17:51
johnmy 发表于 2016-11-9 04:14
as.list
那该怎么去掉list里面的NA呢?

7
jxapp_6451 发表于 2016-11-9 21:29:05
比如

> list(a,b,c)
[[1]]
[1] 120 124 130 136

[[2]]
[1] 122 120  NA  NA

[[3]]
[1] 128 130 134  NA

我想转换成

> list(a,b,c)
[[1]]
[1] 120 124 130 136

[[2]]
[1] 122 120

[[3]]
[1] 128 130 134

这要怎么做

8
zhangyangsmith 发表于 2016-11-9 22:39:09
  1. lapply( list(a,b,c), na.exclude )
复制代码

9
jxapp_6451 发表于 2016-11-10 02:43:30
  1. > lapply( list(a,b,c), na.exclude )
  2. [[1]]
  3. [1] 120 124 130 136

  4. [[2]]
  5. [1] 122 120
  6. attr(,"na.action")
  7. [1] 3 4
  8. attr(,"class")
  9. [1] "exclude"

  10. [[3]]
  11. [1] 128 130 134
  12. attr(,"na.action")
  13. [1] 4
  14. attr(,"class")
  15. [1] "exclude"
复制代码
列表里会多出信息,
试了一下只有这样能返回需要的结果
  1. Dfrm=data.frame(Allele1=a,Allele2=b,Allele3=c)
  2. D=as.list(Dfrm)
  3. D$Allele3=D$Allele3[!is.na(D$Allele3)]
复制代码
然后尝试了这个循环无果
  1. for (i in 1:3){
  2. D[i]=D[i][!is.na(D[i])]
  3. }
复制代码
大神们有知道怎么解决的吗


10
jxapp_6451 发表于 2016-11-11 18:46:29
zhangyangsmith 发表于 2016-11-7 22:23
I would not worry about the attributes. It would not hurt and you will be able to use the elements ...
问题解决了!十分感谢!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 11:41