เปลี่ยนกรอบข้อมูล


107

ฉันต้องการเปลี่ยนกรอบข้อมูลขนาดใหญ่ดังนั้นฉันจึงใช้:

df.aree <- t(df.aree)
df.aree <- as.data.frame(df.aree)

นี่คือสิ่งที่ฉันได้รับ:

df.aree[c(1:5),c(1:5)]
                         10428        10760        12148        11865
    name                M231T3       M961T5       M960T6      M231T19
    GS04.A        5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04
    GS16.A        5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04
    GS20.A        5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04
    GS40.A        3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04    

ปัญหาของฉันคือชื่อคอลัมน์ใหม่ (10428, 10760, 12148, 11865) ที่ฉันต้องกำจัดเพราะฉันต้องใช้แถวแรกเป็นชื่อคอลัมน์

ฉันลองใช้col.names()ฟังก์ชันแล้วแต่ไม่ได้รับสิ่งที่ต้องการ

คุณมีข้อเสนอแนะหรือไม่?

แก้ไข

ขอบคุณสำหรับคำแนะนำ !!! ใช้มันฉันได้รับ:

df.aree[c(1:5),c(1:5)]
                        M231T3       M961T5       M960T6      M231T19
    GS04.A        5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04
    GS16.A        5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04
    GS20.A        5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04
    GS40.A        3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04
    GS44.A        1.225938e+04 2.681887e+03 1.154924e+04 4.202394e+04

ตอนนี้ฉันต้องแปลงชื่อแถว (GS .. ) ในคอลัมน์ปัจจัย ....


1
คุณลองแล้วcolnames(df.aree)<-df.aree[1,];df.aree<-df.aree[2:nrow(df.aree),]หรือยัง?

5
เฟรมข้อมูลไม่ได้ถูกกำหนดให้สามารถเคลื่อนย้ายได้ ถ้าเป็นของคุณบางทีมันควรจะอยู่ในรูปเมทริกซ์แทน
Richie Cotton

ตกลง; tกรอบข้อมูลยังค่อนข้างไม่มีประสิทธิภาพ ถ้าทำได้ให้ใช้เมทริกซ์
mbq

5
การย้าย data.frame ที่มีคอลัมน์สตริงจะเปลี่ยนค่าทั้งหมดเป็นสตริง! ไม่ดี. ดูคำตอบของฉันด้านล่างสำหรับวิธีแก้ปัญหา
Tommy

คำตอบ:


109

คุณไม่ควรเปลี่ยน data.frame ในขณะที่คอลัมน์ชื่ออยู่ในนั้นค่าตัวเลขทั้งหมดจะถูกเปลี่ยนเป็นสตริง!

นี่คือวิธีแก้ปัญหาที่ทำให้ตัวเลขเป็นตัวเลข:

# first remember the names
n <- df.aree$name

# transpose all but the first column (name)
df.aree <- as.data.frame(t(df.aree[,-1]))
colnames(df.aree) <- n
df.aree$myfactor <- factor(row.names(df.aree))

str(df.aree) # Check the column types

49
df.aree <- as.data.frame(t(df.aree))
colnames(df.aree) <- df.aree[1, ]
df.aree <- df.aree[-1, ]
df.aree$myfactor <- factor(row.names(df.aree))

@Riccardo ถ้าเป็นเช่นนั้นให้ยอมรับคำตอบของเขาโดยคลิกที่เครื่องหมายถูกสีเทาข้างๆ
mbq

4
ปัญหาหนึ่งเกี่ยวกับสิ่งนี้ - ชื่อคอลัมน์ใช้แทนตัวเลขของระดับปัจจัย
Harry Palmer

48

คุณสามารถใช้transposeฟังก์ชันจากdata.tableไลบรารี ที่เรียบง่ายและวิธีการแก้ปัญหาได้อย่างรวดเร็วที่ช่วยให้ค่าเป็นnumericnumeric

library(data.table)

# get data
  data("mtcars")

# transpose
  t_mtcars <- transpose(mtcars)

# get row and colnames in order
  colnames(t_mtcars) <- rownames(mtcars)
  rownames(t_mtcars) <- colnames(mtcars)

4
นอกจากนี้ยังsetnames(t_mtcars, rownames(mtcars))จะเป็นdata.table- วิธีการตั้งชื่อบน data.table (และหากใช้data.tableวัตถุคุณจะไม่ตั้งค่าrownames)
SymbolixAU

นี่เป็นทางออกที่ดีที่สุด! +1.
HelloWorld

1

ใช้ประโยชน์จากas.matrix:

# keep the first column 
names <-  df.aree[,1]

# Transpose everything other than the first column
df.aree.T <- as.data.frame(as.matrix(t(df.aree[,-1])))

# Assign first column as the column names of the transposed dataframe
colnames(df.aree.T) <- names
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.