작성자: admin 작성일시: 2016-09-20 00:34:11 조회수: 172 다운로드: 18
카테고리: R 태그목록:

R 패키지 사용하기

패키지 로드

R에서 패키지를 사용하도록 설정하는 것을 패키지 로드(load)라고 한다. 패키지를 로드하려면 다음 명령을 실행한다.

library("패키지이름")

또는

library(패키지이름)

패키지 이름은 따옴표를 해도 되고 안해도 된다.

예를 들어 MASS라는 패키지를 로드하려면 다음과 같이 명령한다.

In [1]:
library(MASS)

현재 로드된 패키지 목록 출력하기

현재 로드되어 있는 패키지의 목록을 출력하려면 cat(.packages())라고 입력한다.

In [2]:
cat(.packages())
MASS stats graphics grDevices utils datasets methods base

패키지 내용 살펴보기

로드된 패키지 안의 내용을 살펴보려면 다음 명령을 사용한다.

ls("package:패키지이름")

예를 들어 방금 로드한 MASS 패키지 안의 내용 목록은 다음과 같이 출력한다.

In [3]:
ls("package:MASS")
  1. 'abbey'
  2. 'accdeaths'
  3. 'addterm'
  4. 'Aids2'
  5. 'Animals'
  6. 'anorexia'
  7. 'area'
  8. 'as.fractions'
  9. 'bacteria'
  10. 'bandwidth.nrd'
  11. 'bcv'
  12. 'beav1'
  13. 'beav2'
  14. 'biopsy'
  15. 'birthwt'
  16. 'Boston'
  17. 'boxcox'
  18. 'cabbages'
  19. 'caith'
  20. 'Cars93'
  21. 'cats'
  22. 'cement'
  23. 'chem'
  24. 'con2tr'
  25. 'contr.sdif'
  26. 'coop'
  27. 'corresp'
  28. 'cov.mcd'
  29. 'cov.mve'
  30. 'cov.rob'
  31. 'cov.trob'
  32. 'cpus'
  33. 'crabs'
  34. 'Cushings'
  35. 'DDT'
  36. 'deaths'
  37. 'denumerate'
  38. 'dose.p'
  39. 'drivers'
  40. 'dropterm'
  41. 'eagles'
  42. 'enlist'
  43. 'epil'
  44. 'eqscplot'
  45. 'farms'
  46. 'fbeta'
  47. 'fgl'
  48. 'fitdistr'
  49. 'forbes'
  50. 'fractions'
  51. 'frequency.polygon'
  52. 'GAGurine'
  53. 'galaxies'
  54. 'gamma.dispersion'
  55. 'gamma.shape'
  56. 'gehan'
  57. 'genotype'
  58. 'geyser'
  59. 'gilgais'
  60. 'ginv'
  61. 'glm.convert'
  62. 'glmmPQL'
  63. 'glm.nb'
  64. 'hills'
  65. 'hist.FD'
  66. 'hist.scott'
  67. 'housing'
  68. 'huber'
  69. 'hubers'
  70. 'immer'
  71. 'Insurance'
  72. 'is.fractions'
  73. 'isoMDS'
  74. 'kde2d'
  75. 'lda'
  76. 'ldahist'
  77. 'leuk'
  78. 'lm.gls'
  79. 'lm.ridge'
  80. 'lmsreg'
  81. 'lmwork'
  82. 'loglm'
  83. 'loglm1'
  84. 'logtrans'
  85. 'lqs'
  86. 'lqs.formula'
  87. 'ltsreg'
  88. 'mammals'
  89. 'mca'
  90. 'mcycle'
  91. 'Melanoma'
  92. 'menarche'
  93. 'michelson'
  94. 'minn38'
  95. 'motors'
  96. 'muscle'
  97. 'mvrnorm'
  98. 'nclass.freq'
  99. 'negative.binomial'
  100. 'neg.bin'
  101. 'negexp.SSival'
  102. 'newcomb'
  103. 'nlschools'
  104. 'npk'
  105. 'npr1'
  106. 'Null'
  107. 'oats'
  108. 'OME'
  109. 'painters'
  110. 'parcoord'
  111. 'petrol'
  112. 'phones'
  113. 'Pima.te'
  114. 'Pima.tr'
  115. 'Pima.tr2'
  116. 'polr'
  117. 'psi.bisquare'
  118. 'psi.hampel'
  119. 'psi.huber'
  120. 'qda'
  121. 'quine'
  122. 'Rabbit'
  123. 'rational'
  124. 'renumerate'
  125. 'rlm'
  126. 'rms.curv'
  127. 'rnegbin'
  128. 'road'
  129. 'rotifer'
  130. 'Rubber'
  131. 'sammon'
  132. 'select'
  133. 'Shepard'
  134. 'ships'
  135. 'shoes'
  136. 'shrimp'
  137. 'shuttle'
  138. 'Sitka'
  139. 'Sitka89'
  140. 'Skye'
  141. 'snails'
  142. 'SP500'
  143. 'stdres'
  144. 'steam'
  145. 'stepAIC'
  146. 'stormer'
  147. 'studres'
  148. 'survey'
  149. 'synth.te'
  150. 'synth.tr'
  151. 'theta.md'
  152. 'theta.ml'
  153. 'theta.mm'
  154. 'topo'
  155. 'Traffic'
  156. 'truehist'
  157. 'ucv'
  158. 'UScereal'
  159. 'UScrime'
  160. 'VA'
  161. 'waders'
  162. 'whiteside'
  163. 'width.SJ'
  164. 'write.matrix'
  165. 'wtloss'

만약 함수의 목록만 출력하라면 다음과 같이 명령한다.

In [4]:
lsf.str("package:MASS")
addterm : function (object, ...)  
area : function (f, a, b, ..., fa = f(a, ...), fb = f(b, ...), limit = 10, eps = 1e-05)  
as.fractions : function (x)  
bandwidth.nrd : function (x)  
bcv : function (x, nb = 1000, lower = 0.1 * hmax, upper = hmax)  
boxcox : function (object, ...)  
con2tr : function (obj)  
contr.sdif : function (n, contrasts = TRUE, sparse = FALSE)  
corresp : function (x, ...)  
cov.mcd : function (...)  
cov.mve : function (...)  
cov.rob : function (x, cor = FALSE, quantile.used = floor((n + p + 1)/2), method = c("mve", 
    "mcd", "classical"), nsamp = "best", seed)  
cov.trob : function (x, wt = rep(1, n), cor = FALSE, center = TRUE, nu = 5, maxit = 25, 
    tol = 0.01)  
denumerate : function (x)  
dose.p : function (obj, cf = 1:2, p = 0.5)  
dropterm : function (object, ...)  
enlist : function (vec)  
eqscplot : function (x, y, ratio = 1, tol = 0.04, uin, ...)  
fbeta : function (x, alpha, beta)  
fitdistr : function (x, densfun, start, ...)  
fractions : function (x, cycles = 10, max.denominator = 2000, ...)  
frequency.polygon : function (x, nclass = nclass.freq(x), xlab = "", ylab = "", ...)  
gamma.dispersion : function (object, ...)  
gamma.shape : function (object, ...)  
ginv : function (X, tol = sqrt(.Machine$double.eps))  
glm.convert : function (object)  
glmmPQL : function (fixed, random, family, data, correlation, weights, control, niter = 10, 
    verbose = TRUE, ...)  
glm.nb : function (formula, data, weights, subset, na.action, start = NULL, etastart, 
    mustart, control = glm.control(...), method = "glm.fit", model = TRUE, 
    x = FALSE, y = TRUE, contrasts = NULL, ..., init.theta, link = log)  
hist.FD : function (x, prob = TRUE, xlab = deparse(substitute(x)), ...)  
hist.scott : function (x, prob = TRUE, xlab = deparse(substitute(x)), ...)  
huber : function (y, k = 1.5, tol = 1e-06)  
hubers : function (y, k = 1.5, mu, s, initmu = median(y), tol = 1e-06)  
is.fractions : function (f)  
isoMDS : function (d, y = cmdscale(d, k), k = 2, maxit = 50, trace = TRUE, tol = 0.001, 
    p = 2)  
kde2d : function (x, y, h, n = 25, lims = c(range(x), range(y)))  
lda : function (x, ...)  
ldahist : function (data, g, nbins = 25, h, x0 = -h/1000, breaks, xlim = range(breaks), 
    ymax = 0, width, type = c("histogram", "density", "both"), sep = (type != 
        "density"), col = 5L, xlab = deparse(substitute(data)), bty = "n", 
    ...)  
lm.gls : function (formula, data, W, subset, na.action, inverse = FALSE, method = "qr", 
    model = FALSE, x = FALSE, y = FALSE, contrasts = NULL, ...)  
lm.ridge : function (formula, data, subset, na.action, lambda = 0, model = FALSE, 
    x = FALSE, y = FALSE, contrasts = NULL, ...)  
lmsreg : function (...)  
lmwork : function (object)  
loglm : function (formula, data, subset, na.action, ...)  
loglm1 : function (formula, data, ...)  
logtrans : function (object, ...)  
lqs : function (x, ...)  
lqs.formula : function (formula, data, ..., method = c("lts", "lqs", "lms", "S", "model.frame"), 
    subset, na.action, model = TRUE, x.ret = FALSE, y.ret = FALSE, contrasts = NULL)  
ltsreg : function (...)  
mca : function (df, nf = 2, abbrev = FALSE)  
mvrnorm : function (n = 1, mu, Sigma, tol = 1e-06, empirical = FALSE, EISPACK = FALSE)  
nclass.freq : function (x)  
negative.binomial : function (theta = stop("'theta' must be specified"), link = "log")  
neg.bin : function (theta = stop("'theta' must be given"))  
negexp.SSival : function (mCall, data, LHS)  
Null : function (M)  
parcoord : function (x, col = 1, lty = 1, var.label = FALSE, ...)  
polr : function (formula, data, weights, start, ..., subset, na.action, contrasts = NULL, 
    Hess = FALSE, model = TRUE, method = c("logistic", "probit", "loglog", 
        "cloglog", "cauchit"))  
psi.bisquare : function (u, c = 4.685, deriv = 0)  
psi.hampel : function (u, a = 2, b = 4, c = 8, deriv = 0)  
psi.huber : function (u, k = 1.345, deriv = 0)  
qda : function (x, ...)  
rational : function (x, cycles = 10, max.denominator = 2000, ...)  
renumerate : function (x)  
rlm : function (x, ...)  
rms.curv : function (obj)  
rnegbin : function (n, mu = n, theta = stop("'theta' must be specified"))  
sammon : function (d, y = cmdscale(d, k), k = 2, niter = 100, trace = TRUE, magic = 0.2, 
    tol = 1e-04)  
select : function (obj)  
Shepard : function (d, x, p = 2)  
stdres : function (object)  
stepAIC : function (object, scope, scale = 0, direction = c("both", "backward", "forward"), 
    trace = 1, keep = NULL, steps = 1000, use.start = FALSE, k = 2, ...)  
studres : function (object)  
theta.md : function (y, mu, dfr, weights, limit = 20, eps = .Machine$double.eps^0.25)  
theta.ml : function (y, mu, n = sum(weights), weights, limit = 10, eps = .Machine$double.eps^0.25, 
    trace = FALSE)  
theta.mm : function (y, mu, dfr, weights, limit = 10, eps = .Machine$double.eps^0.25)  
truehist : function (data, nbins = "Scott", h, x0 = -h/1000, breaks, prob = TRUE, 
    xlim = range(breaks), ymax = max(est), col = "cyan", xlab = deparse(substitute(data)), 
    bty = "n", ...)  
ucv : function (x, nb = 1000, lower = 0.1 * hmax, upper = hmax)  
width.SJ : function (x, nb = 1000, lower = 0.1 * hmax, upper = hmax, method = c("ste", 
    "dpi"))  
write.matrix : function (x, file = "", sep = " ", blocksize)  

패키지에 포함된 함수 등을 사용하기

패키지에 포함된 함수 등은 패키지가 로드되면 바로 사용할 수 있다.

In [5]:
boxcox(Volume ~ log(Height) + log(Girth), data = trees,
       lambda = seq(-0.25, 0.25, length = 10))

패키지에 포함된 데이터 사용하기

대부분의 R 패키지에는 샘플용 데이터 셋도 같이 포함되어 있다. 현재 로드된 패키지에 포함된 데이터의 목록을 출력하려면 data() 명령을 사용한다. 주피터 노트북에서는 페이저(pager)화면으로 나타난다. 로드 여부에 상관없이 특정 패키지에 포함된 데이터 목록을 출력하려면 data(package="packagename") 명령을 사용한다.

예를 들어 MASS 패키지에는 Motor Trend Car Road Tests 라는 자동차 관련 데이터가 mtcars 라는 이름으로 들어있다.

In [6]:
mtcars
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX421.0 6 160.0110 3.90 2.62016.460 1 4 4
Mazda RX4 Wag21.0 6 160.0110 3.90 2.87517.020 1 4 4
Datsun 71022.8 4 108.0 93 3.85 2.32018.611 1 4 1
Hornet 4 Drive21.4 6 258.0110 3.08 3.21519.441 0 3 1
Hornet Sportabout18.7 8 360.0175 3.15 3.44017.020 0 3 2
Valiant18.1 6 225.0105 2.76 3.46020.221 0 3 1
Duster 36014.3 8 360.0245 3.21 3.57015.840 0 3 4
Merc 240D24.4 4 146.7 62 3.69 3.19020.001 0 4 2
Merc 23022.8 4 140.8 95 3.92 3.15022.901 0 4 2
Merc 28019.2 6 167.6123 3.92 3.44018.301 0 4 4
Merc 280C17.8 6 167.6123 3.92 3.44018.901 0 4 4
Merc 450SE16.4 8 275.8180 3.07 4.07017.400 0 3 3
Merc 450SL17.3 8 275.8180 3.07 3.73017.600 0 3 3
Merc 450SLC15.2 8 275.8180 3.07 3.78018.000 0 3 3
Cadillac Fleetwood10.4 8 472.0205 2.93 5.25017.980 0 3 4
Lincoln Continental10.4 8 460.0215 3.00 5.42417.820 0 3 4
Chrysler Imperial14.7 8 440.0230 3.23 5.34517.420 0 3 4
Fiat 12832.4 4 78.7 66 4.08 2.20019.471 1 4 1
Honda Civic30.4 4 75.7 52 4.93 1.61518.521 1 4 2
Toyota Corolla33.9 4 71.1 65 4.22 1.83519.901 1 4 1
Toyota Corona21.5 4 120.1 97 3.70 2.46520.011 0 3 1
Dodge Challenger15.5 8 318.0150 2.76 3.52016.870 0 3 2
AMC Javelin15.2 8 304.0150 3.15 3.43517.300 0 3 2
Camaro Z2813.3 8 350.0245 3.73 3.84015.410 0 3 4
Pontiac Firebird19.2 8 400.0175 3.08 3.84517.050 0 3 2
Fiat X1-927.3 4 79.0 66 4.08 1.93518.901 1 4 1
Porsche 914-226.0 4 120.3 91 4.43 2.14016.700 1 5 2
Lotus Europa30.4 4 95.1113 3.77 1.51316.901 1 5 2
Ford Pantera L15.8 8 351.0264 4.22 3.17014.500 1 5 4
Ferrari Dino19.7 6 145.0175 3.62 2.77015.500 1 5 6
Maserati Bora15.0 8 301.0335 3.54 3.57014.600 1 5 8
Volvo 142E21.4 4 121.0109 4.11 2.78018.601 1 4 2

질문/덧글

아직 질문이나 덧글이 없습니다. 첫번째 글을 남겨주세요!