在 r 中写出 .dat 文件
writing out .dat file in r
我的数据集如下所示:
ids <- c(111,12,134,14,155,16,17,18,19,20)
scores.1 <- c(0,1,0,1,1,2,0,1,1,1)
scores.2 <- c(0,0,0,1,1,1,1,1,1,0)
data <- data.frame(ids, scores.1, scores.1)
> data
ids scores.1 scores.1.1
1 111 0 0
2 12 1 1
3 134 0 0
4 14 1 1
5 155 1 1
6 16 2 2
7 17 0 0
8 18 1 1
9 19 1 1
10 20 1 1
ids
代表学号,scores.1
是第一题的response/score,scores.2
是第二题的response/score。学生 ID 的位数有所不同,但分数始终为 1 位。我试图通过生成一些对象并在 gdata
库中的 write.fwf
函数中使用这些对象来写出 .dat
文件。
item.count <- dim(data)[2] - 1 # counts the number of questions in the dataset
write.fwf(data, file = "data.dat", width = c(5,rep(1, item.count)),
colnames = FALSE, sep = "")
我想用一些空格将学生ID和问题回答分开,所以我想为学生ID使用5个空格并指定我在write.fwf()
函数中使用了width = c(5, rep(1, item.count))
。但是,输出文件看起来像这样,学生 ID 左侧有空格
11100
1211
13400
1411
15511
1622
1700
1811
1911
2011
而不是在 ids
的右侧。
111 00
12 11
134 00
14 11
155 11
16 22
17 00
18 11
19 11
20 11
有什么建议吗?
谢谢!
我们可以使用 unite
将 unite
的 'score' 列合并为一个,然后使用 write.csv
library(dplyr)
library(tidyr)
data %>%
unite(scores, starts_with('scores'), sep='')
在@akrun 的帮助下,这给出了我想要的:
library(dplyr)
library(tidyr)
data %>%
unite(scores, starts_with('scores'), sep='')
write.fwf(data, file = "data.dat",
width = c(5,item.count),
colnames = FALSE, sep = " ")
在 .dat 文件中,数据集如下所示:
111 00
12 11
134 00
14 11
155 11
16 22
17 00
18 11
19 11
20 11
我的数据集如下所示:
ids <- c(111,12,134,14,155,16,17,18,19,20)
scores.1 <- c(0,1,0,1,1,2,0,1,1,1)
scores.2 <- c(0,0,0,1,1,1,1,1,1,0)
data <- data.frame(ids, scores.1, scores.1)
> data
ids scores.1 scores.1.1
1 111 0 0
2 12 1 1
3 134 0 0
4 14 1 1
5 155 1 1
6 16 2 2
7 17 0 0
8 18 1 1
9 19 1 1
10 20 1 1
ids
代表学号,scores.1
是第一题的response/score,scores.2
是第二题的response/score。学生 ID 的位数有所不同,但分数始终为 1 位。我试图通过生成一些对象并在 gdata
库中的 write.fwf
函数中使用这些对象来写出 .dat
文件。
item.count <- dim(data)[2] - 1 # counts the number of questions in the dataset
write.fwf(data, file = "data.dat", width = c(5,rep(1, item.count)),
colnames = FALSE, sep = "")
我想用一些空格将学生ID和问题回答分开,所以我想为学生ID使用5个空格并指定我在write.fwf()
函数中使用了width = c(5, rep(1, item.count))
。但是,输出文件看起来像这样,学生 ID 左侧有空格
11100
1211
13400
1411
15511
1622
1700
1811
1911
2011
而不是在 ids
的右侧。
111 00
12 11
134 00
14 11
155 11
16 22
17 00
18 11
19 11
20 11
有什么建议吗?
谢谢!
我们可以使用 unite
将 unite
的 'score' 列合并为一个,然后使用 write.csv
library(dplyr)
library(tidyr)
data %>%
unite(scores, starts_with('scores'), sep='')
在@akrun 的帮助下,这给出了我想要的:
library(dplyr)
library(tidyr)
data %>%
unite(scores, starts_with('scores'), sep='')
write.fwf(data, file = "data.dat",
width = c(5,item.count),
colnames = FALSE, sep = " ")
在 .dat 文件中,数据集如下所示:
111 00
12 11
134 00
14 11
155 11
16 22
17 00
18 11
19 11
20 11