0
votes

I am trying to create numerical prefixes to rownames in R.

What I have is the following:

see png

enter image description here

..and what I would need is the following:

http://www.wordfish.org/uploads/1/2/9/8/12985397/wcger_alldim_chop.csv

The Format is a term-document Matrix or just simply an R Matrix.

I would need the Format to use the program "wordfish" (text mining).

Format should look exactly like this. I was able to create the matrix as can be seen in the link below but fail to add the numerical counts in front of each word as well as assign quotes around the number and the word itself.

Unfortunately,

write.table(wcdata, file = "test.csv", row.names = TRUE, col.names = NA, qmethod = c("double"))

and

write.csv(wcdata, quote = TRUE, row.names=TRUE, "test.csv")

only place quotes around the columns (which is good of course)

Thanks in advance for any help

2
Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples. - Abdullah Khan

2 Answers

0
votes

I hope I got your issue but I'm not quite sure.

r1 <- c("alt", 1,2,3,4,5,6,7)
r2 <- c("neu", 2,3,4,5,6,7,8)
r3 <- c("zu", 1,1,1,1,1,1,1)
r4 <- c("hier", 1,2,1,2,1,2,1)
r5 <- c("das", 4,3,4,3,4,3,4)
r6 <- c("die", 7,7,7,7,7,7,7)
r7 <- c("der", 2,5,4,4,5,7,6)

tdm <- rbind(r1,r2,r3,r4,r5,r6,r7)

tdm2 <- cbind(c(1:nrow(tdm)),tdm)
tdm2[,1] <- sapply(tdm2[,1], function(x) paste('\"', x,'\"',sep = ""))
tdm2[,2] <- sapply(tdm2[,2], function(x) paste('\"', x,'\"',sep = ""))

I think the last 3 rows are interesting for you. The others are just for my example because you provided no data.

0
votes

Thank you very much for the quick response.

The solution goes in the right direction but is not exactly what I would need.

The data was provided with a screen shot:

see here:

TDM_1

What i get when executing the two lines of code you provide

wcdata_2 <- cbind(c(1:nrow(wcdata)),wcdata)
wcdata_2[,1] <- sapply(wcdata_2[,1], function(x) paste('\"', x,'\"',sep = ""))

is the following

TDM 2

which goes into the right direction but what I would need is a prefix (number) in front of every row and then quoting both the prefix and the rowinformation itself as can bee seen in the link above.

So:

 r1 <- c("1" "alt", 1,2,3,4,5,6,7)  
 r2 <- c("2" "neu", 2,3,4,5,6,7,8)  
 r3 <- c("3" "zu", 1,1,1,1,1,1,1)  
 r4 <- c("4" "hier", 1,2,1,2,1,2,1)  
 r5 <- c("5" "das", 4,3,4,3,4,3,4)  
 r6 <- c("6" "die", 7,7,7,7,7,7,7)  
 r7 <- c("7" "der", 2,5,4,4,5,7,6)  

So one row is one word count and the respective word per document and the documents are the columns. The words are provided by the tdm but the counts and the quotes around the prefix and the words are not.

I hope this makes it more clear.