Reputation: 131
I have a data that looks like this.For example:
A;a
B;a
C;b
D;c
A;b
A;d
C;c
....
First pos = key , second pos = value. If key; value , then 1 ,if not then 0;
I would to create binary matrix from data.
a b c d
A 1 1 0 1
B 1 0 0 0
C 0 1 1 0
D 0 0 0 1
I could create matrix, my code:
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)
Now, i want to re-write to .txt format in this form:
A;a;1
A;b;1
A;c;0
A;d;1
B;a;1
B;b;0
B;c;0
B;d;0
.....
My code:
t3<-tableForData[,]
View(t3)
then i view table in this form
How i can write in this form into .txt file?
Upvotes: 3
Views: 27488
Reputation: 6459
(edited for better consistency with the OP)
In base R you can do it in just one line using 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 :)
Then t3 is...
> 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
You can sort it if the order of rows is important:
t3 <- t3[order(t3$V1),]
... and write into a file:
write.table(t3, "afilename.csv", sep=";", col.names=FALSE, quote=FALSE, row.names=FALSE)
Upvotes: 3
Reputation: 9413
A third option would be to use tidyr
library(tidyr)
library(dplyr)
library(tibble)
long_data = data.frame(X1=c("A", "B", "C", "D"), X2=c("a", "b", "c", "d"))
long_data %>%
spread(X1, 1) %>%
mutate_at(vars(-X2), funs(if_else(is.na(.), 0, 1))) %>%
column_to_rownames("X2") %>%
write.table(file="TestData.csv", quote=F, sep=";")
# A;B;C;D
# a;1;0;0;0
# b:0;1;0;0
# c;0;0;1;0
# d;0;0;0;1
expand.grid(long_data) %>%
left_join(long_data %>% mutate(val=1)) %>%
replace_na(list(val=0)) %>%
write.table(file="TestData2.csv", quote=F, sep=";")
# X1;X2;val
# A;a;1
# B;a;0
# C;a;0
# D;a;0
# A;b;0
# B;b;1
# C;b;0
# D;b;0
# A;c;0
# B;c;0
# C;c;1
# D;c;0
# A;d;0
# B;d;0
# C;d;0
# D;d;1
Upvotes: 2
Reputation: 4482
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)
Edit after @MichaelChirico suggestion
Instead of
write.table(dt_m,"test.txt", sep=";",col.names = FALSE, row.names = FALSE)
it is better to use
fwrite(dt_m,"test.txt", sep=";",col.names = FALSE, row.names = FALSE)
Upvotes: 5
Reputation: 3038
You could use reshape2::melt
to reshape the data into the form you want, then save the table without row or column names.
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)
Upvotes: 1