【问题标题】:Read data from text file and sort into a vector [closed]从文本文件中读取数据并排序为向量[关闭]
【发布时间】:2013-01-09 15:55:11
【问题描述】:

我有一个像这样的数据文件...

Control,3
Test,3
Control,3
Control,3
Test,1
Control,3
Control,1

我想将它们加载到 2 个向量测试和控制中。 有什么想法吗?

【问题讨论】:

  • 你试过什么?你能提供一个reproducible example吗?
  • 我用过 mydata = read.csv("c:/Users/james/Desktop/4sec_vs_2sec_Data_Rate.txt")

标签: r file vector


【解决方案1】:

试试read.csv()

dat <- read.csv(text = "Control,3
Test,3
Control,3
Control,3
Test,1
Control,3
Control,1", header = FALSE)

> dat
       V1 V2
1 Control  3
2    Test  3
3 Control  3
4 Control  3
5    Test  1
6 Control  3
7 Control  1

但是,您需要 read.csv(file = "foo.csv", header = FALSE) 并将 foo.csv 替换为文件的路径和名称。

然后

test <- with(dat, V2[V1 == "Test"])
control <- with(dat, V2[V1 == "Control"])

> test
[1] 3 1
> control
[1] 3 3 3 3 1

假设我已经明白你想要什么?

【讨论】:

  • 呃,testcontrol 是这样的; 2 个向量。要获得平均值或 SD,请执行 mean(control)sd(test) 等。
猜你喜欢
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多