आर

2011-07-27 14 views
6

में निर्यात मैट्रिक्स मैं आर में एक मैट्रिक्स निर्यात करना चाहता हूं (और मेरी पंक्तियों और कॉलम दोनों के नाम रखें)। जब मैं write.table या write.csv का उपयोग करता हूं तो मुझे एक नए कॉलम के साथ एक मैट्रिक्स मिलता है। मैं इस फ़ंक्शन का उपयोग कैसे कर सकता हूं।आर

आपकी मदद के लिए धन्यवाद।

उत्तर

11

मैं समस्या को देखने में विफल रहता हूं। आपको एक नया कॉलम नहीं मिलता है, पंक्ति नाम टेक्स्ट फ़ाइल में पहले कॉलम के रूप में सहेजे जाते हैं। तो या तो आप उस कॉलम को निर्दिष्ट करते हैं जहां read.table में पंक्ति नाम दिए गए हैं, या विकल्प write.table में उपयोग करें।

प्रदर्शन:

mat <- matrix(1:10,ncol=2) 
rownames(mat) <- letters[1:5] 
colnames(mat) <- LETTERS[1:2] 

mat 
write.table(mat,file="test.txt") # keeps the rownames 
read.table("test.txt",header=TRUE,row.names=1) # says first column are rownames 
unlink("test.txt") 
write.table(mat,file="test2.txt",row.names=FALSE) # drops the rownames 
read.table("test.txt",header=TRUE) 
unlink("test2.txt") 

किसी भी मामले में, मदद फ़ाइलों को पढ़ने के आप यह सब बता दिया होता।

+1

+1 मदद फाइलों की प्रतिलिपि बनाने के लिए –

+3

इस बारे में कठोर होने की आवश्यकता नहीं है - उदा। मैंने दस्तावेज़ों की जांच की, लेकिन write.matrix के लिए, और इसमें ऐसा कोई विकल्प नहीं है –

2

मुझे लगता है कि "नया कॉलम" से आपका मतलब पंक्ति नाम है जो डिफ़ॉल्ट रूप से लिखा जाता है। उन्हें दबाने के लिए, सेट करें जब write.table या write.csv पर कॉल करें।

write.table    package:utils    R Documentation 

Data Output 

Description: 

    ‘write.table’ prints its required argument ‘x’ (after converting 
    it to a data frame if it is not one nor a matrix) to a file or 
    connection. 

Usage: 

    write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", 
       eol = "\n", na = "NA", dec = ".", row.names = TRUE, 
       col.names = TRUE, qmethod = c("escape", "double")) 

    write.csv(...) 
    write.csv2(...) 

... 

row.names: either a logical value indicating whether the row names of 
      ‘x’ are to be written along with ‘x’, or a character vector 
      of row names to be written. 

col.names: either a logical value indicating whether the column names 
      of ‘x’ are to be written along with ‘x’, or a character 
      vector of column names to be written. See the section on 
      ‘CSV files’ for the meaning of ‘col.names = NA’. 
+0

हेहे, वास्तव में मदद फ़ाइलों की प्रतिलिपि बनाने के लिए +1। लेकिन 'लिखने योग्य' पर्याप्त होगा ... –