Programing

데이터 프레임 전치

lottogame 2020. 8. 28. 07:48
반응형

데이터 프레임 전치


큰 데이터 프레임을 전치해야하므로 다음을 사용했습니다.

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 ..)을 변환해야합니다 ....


name 열이있는 동안에는 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

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))

라이브러리 transpose기능을 사용할 수 있습니다 data.table. 유지 간단하고 빠른 솔루션 numeric으로 값을 numeric.

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)

참고URL : https://stackoverflow.com/questions/6778908/transpose-a-data-frame

반응형