티스토리 뷰

차기작 : R을 배우자

I have a table!

quantlab 2015. 11. 11. 20:09

나에게 표가 하나 있다. 예전에 만들었는데, 그 때는 평균과 표준편차를 한 테이블에 넣으려고 애를 썼던 기억이 있다. 이제 세월이 흘러 평균과 괄호 안의 표준편차를 분리해야 할 일이 생겼다. 인생의 아이러니란!

I have a table. I created it several months ago and I remember trying to come up with a way to put means and sds in one table. Now I have to seperate those two, means and sds that are inside the parentheses. Time flies!


표다. Here's the table.

 

norm

extreme

inv.ext

straight

random-unif

random-norm cat-restric
sd 0.97(0.16) 1.35(0.27) 0.59(0.11) 0.87(0.27) 1.41(0.14) 1(0.15) 0.81(0.07)
entropy 1.81(0.24) 1.85(0.41) 1.19(0.24) 1.48(0.37) 2.17(0.11) 1.88(0.21) 1.51(0.08)
lz 0.99(0.08) 0.99(0.12) 0.83(0.08) 0.89(0.11) 1.07(0.05) 1(0.07) 0.92(0.05)
maxrle 3.29(1.2) 3.8(2.29) 5.36(2.26) 7.54(2.87) 2.59(0.77) 3.16(1.09) 3.38(1.08)
c-prop 0.67(0.11) 0.66(0.16) 0.49(0.13) 0.27(0.1) 0.76(0.09) 0.69(0.1) 0.63(0.1)
middle 0.33(0.13) 0.15(0.09) 0.6(0.15) 0.33(0.21) 0.2(0.09) 0.38(0.11) 0.33(0.11)
extreme 0.22(0.13) 0.57(0.14) 0.02(0.04) 0.22(0.2) 0.4(0.11) 0.13(0.08) 0(0)


이것을 Copy and Paste를 하면 다음의 둘 중 하나의 text를 얻을 수 있다.

If you copy and paste it, we get one of two texts below.


txt <- "0.97(0.16)  1.35(0.27) 0.59(0.11) 0.87(0.27) 1.41(0.14) 1(0.15) 0.81(0.07)

1.81(0.24) 1.85(0.41) 1.19(0.24) 1.48(0.37) 2.17(0.11) 1.88(0.21) 1.51(0.08)

0.99(0.08) 0.99(0.12) 0.83(0.08) 0.89(0.11) 1.07(0.05) 1(0.07) 0.92(0.05)

3.29(1.2) 3.8(2.29) 5.36(2.26) 7.54(2.87) 2.59(0.77) 3.16(1.09) 3.38(1.08)

0.67(0.11) 0.66(0.16) 0.49(0.13) 0.27(0.1) 0.76(0.09) 0.69(0.1) 0.63(0.1)

0.33(0.13) 0.15(0.09) 0.6(0.15) 0.33(0.21) 0.2(0.09) 0.38(0.11) 0.33(0.11)

0.22(0.13) 0.57(0.14) 0.02(0.04) 0.22(0.2) 0.4(0.11) 0.13(0.08) 0(0)"


txt <- 

"0.97(0.16)

1.35(0.27)

0.59(0.11)

0.87(0.27)

1.41(0.14)

1(0.15)

0.81(0.07)

1.81(0.24)

1.85(0.41)

1.19(0.24)

1.48(0.37)

2.17(0.11)

1.88(0.21)

1.51(0.08)

0.99(0.08)

0.99(0.12)

0.83(0.08)

0.89(0.11)

1.07(0.05)

1(0.07)

0.92(0.05)

3.29(1.2)

3.8(2.29)

5.36(2.26)

7.54(2.87)

2.59(0.77)

3.16(1.09)

3.38(1.08)

0.67(0.11)

0.66(0.16)

0.49(0.13)

0.27(0.1)

0.76(0.09)

0.69(0.1)

0.63(0.1)

0.33(0.13)

0.15(0.09)

0.6(0.15)

0.33(0.21)

0.2(0.09)

0.38(0.11)

0.33(0.11)

0.22(0.13)

0.57(0.14)

0.02(0.04)

0.22(0.2)

0.4(0.11)

0.13(0.08)

0(0)"


어느 쪽이든 다음의 코드를 통해 long form으로 저장할 수 있고, dcast 명령을 통해 다시 wide form으로 변형시킬 수 있다.

Either way is okay. The code below converts them into a data frame and if you want you can use "dcast" to make it into the wide form.


v.txt <- unlist(strsplit(txt, "[[:space:]]+")) # + means one or more than one letter


inside_sd <- gsub(".*\\((.*)\\).*", "\\1", v.txt) 

outside_mean <- gsub("(.*)\\((.*)\\).*", "\\1", v.txt)


grid <- expand.grid( c("norm","extreme","inv.ext","straight","random-unif","random-norm","cat-restric"),

                     c('sd','entropy','lz','maxrle','c-prop','middle','extreme'))


simpleStats <- data.frame(resp=grid$Var1, 

                          stat=grid$Var2,

                          mean=as.numeric(outside_mean),

                          sd=as.numeric(inside_sd))


#simpleStats <- simpleStats[simpleStats$stat != "maxrle",]


library(reshape2)

dcast(simpleStats, stat ~ resp, value.var="mean")

dcast(simpleStats, stat ~ resp, value.var="sd")


[:space:]는 숫자 혹은 문자 사이의 공간을 나타낸다.

[:space:] is a pre-defined character class that matches space characters in your locale.

http://stackoverflow.com/questions/10502787/removing-trailing-spaces-with-gsub-in-r

우선 공간을 기준으로 문자열을 나누고, 괄호 안과 괄호 밖의 숫자를 추출한다.

First strsplit the txt by the spaces, and pick out numbers inside and outside the parenthesis, which is now stored in inside_sd, outside_mean.


expand.grid는 grid를 expand 한다.

expand.grid expands input to a grid.


그 다음부터는 알아서...

And the rest of the code is a homework...


'차기작 : R을 배우자' 카테고리의 다른 글

Using mirt model fit for PerFit::lz & lzstar  (0) 2016.03.16
fitting RSM  (0) 2016.01.30
Naming files by date  (0) 2015.11.11
Dealing with multidimensional array  (0) 2015.11.10
8 dimensions  (0) 2015.11.02
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함