我有一个看起来像这样的数据。例如:
A;a
B;a
C;b
D;c
A;b
A;d
C;c
....
第一个pos = key,第二个pos = value。如果关键;值,然后是1,如果不是那么0;
我想从数据创建二进制矩阵。
a b c d
A 1 1 0 1
B 1 0 0 0
C 0 1 1 0
D 0 0 0 1
我可以创建矩阵,我的代码:
KeyandValue = read.table('~/RStudioProjects/TestData.txt',sep=';',header = FALSE)
tableForData <- table(KeyandValue$V1,KeyandValue$V2)
tableForData[tableForData > 1] <- 1
csvFile<- write.table(tableForData,file =
"~/RStudioProjects/TestData.csv",quote = F,sep = ";")
write.csv(csvFile)
现在,我想以这种形式重写为.txt格式:
A;a;1
A;b;1
A;c;0
A;d;1
B;a;1
B;b;0
B;c;0
B;d;0
.....
我的代码:
t3<-tableForData[,]
View(t3)
我如何用这种形式写入.txt文件?
答案 0 :(得分:2)
library(data.table)
dt <- data.table("id" = c("A","B","C","D"),
"a" = c(1,1,0,0),
"b" = c(1,0,1,0),
"c" = c(0,0,1,0),
"d" = c(1,0,0,1))
dt_m <- melt.data.table(dt,id.vars = "id")
write.table(dt_m,"test.txt", sep=";",col.names = FALSE, row.names = FALSE)
在@MichaelChirico建议之后编辑
而不是
write.table(dt_m,"test.txt", sep=";",col.names = FALSE, row.names = FALSE)
最好使用
fwrite(dt_m,"test.txt", sep=";",col.names = FALSE, row.names = FALSE)
答案 1 :(得分:1)
您可以使用reshape2::melt
将数据重新整形为所需的表单,然后保存表格而不使用行名或列名。
df <- data.frame(lapply(1:4, function(i) sample(1:0, 4, replace=TRUE)))
colnames(df) <- letters[1:4]
rownames(df) <- LETTERS[1:4]
#> df
#
# a b c d
#A 1 0 1 0
#B 1 1 1 0
#C 1 0 1 1
#D 1 0 0 1
## Saving to disk
df$id <- rownames(df)
write.table(reshape2::melt(df), sep=";", col.names=FALSE, row.names=FALSE)
答案 2 :(得分:1)
第三种选择是使用tidyr
Search: "Mar"
Results:
+---------------------------------------+
| 1 Mark Operator OperatorOptions |
| 2 Mary Manager ManagerOptions |
+---------------------------------------+
答案 3 :(得分:1)
(为了更好地与OP保持一致而编辑)
在基础R中,您可以使用as.data.frame.table
:
df <- data.frame(
V1 = c("A", "B", "C", "D", "A", "A", "C"),
V2 = c("a", "a", "b", "c", "b", "d", "c"))
tableForData <- with(df, table(V1,V2))
tableForData[tableForData > 1] <- 1
t3 <- as.data.frame(tableForData) #this is the working part :)
然后 t3 是......
> head(t3)
V1 V2 Freq
1 A a 1
2 B a 1
3 C a 0
4 D a 0
5 A b 1
6 B b 0
如果行的顺序很重要,您可以对其进行排序:
t3 <- t3[order(t3$V1),]
...并写入文件:
write.table(t3, "afilename.csv", sep=";", col.names=FALSE, quote=FALSE, row.names=FALSE)