본문 바로가기
R

[R] 필수 패키지 설치 및 외부 문서 읽어오기

by 빵으니 2020. 7. 2.
Chap01

필수 패키지 설치

In [1]:
install.packages('ggplot2')
also installing the dependencies 'desc', 'pkgbuild', 'rprojroot', 'pkgload', 'praise', 'rlang', 'testthat', 'isoband'

package 'desc' successfully unpacked and MD5 sums checked
package 'pkgbuild' successfully unpacked and MD5 sums checked
package 'rprojroot' successfully unpacked and MD5 sums checked
package 'pkgload' successfully unpacked and MD5 sums checked
package 'praise' successfully unpacked and MD5 sums checked
package 'rlang' successfully unpacked and MD5 sums checked
package 'testthat' successfully unpacked and MD5 sums checked
package 'isoband' successfully unpacked and MD5 sums checked
package 'ggplot2' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [2]:
install.packages('ggthemes')
package 'ggthemes' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [3]:
install.packages('data.table')
package 'data.table' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [4]:
install.packages('devtools')
also installing the dependencies 'ini', 'fs', 'gh', 'rematch2', 'processx', 'rex', 'crosstalk', 'xopen', 'brew', 'commonmark', 'purrr', 'usethis', 'callr', 'cli', 'covr', 'digest', 'DT', 'ellipsis', 'glue', 'git2r', 'httr', 'jsonlite', 'memoise', 'rcmdcheck', 'remotes', 'roxygen2', 'rstudioapi', 'rversions', 'sessioninfo'

  There is a binary version available but the source version is later:
   binary source needs_compilation
fs  1.4.1  1.4.2              TRUE

  Binaries will be installed
package 'ini' successfully unpacked and MD5 sums checked
package 'fs' successfully unpacked and MD5 sums checked
package 'gh' successfully unpacked and MD5 sums checked
package 'rematch2' successfully unpacked and MD5 sums checked
package 'processx' successfully unpacked and MD5 sums checked
package 'rex' successfully unpacked and MD5 sums checked
package 'crosstalk' successfully unpacked and MD5 sums checked
package 'xopen' successfully unpacked and MD5 sums checked
package 'brew' successfully unpacked and MD5 sums checked
package 'commonmark' successfully unpacked and MD5 sums checked
package 'purrr' successfully unpacked and MD5 sums checked
package 'usethis' successfully unpacked and MD5 sums checked
package 'callr' successfully unpacked and MD5 sums checked
package 'cli' successfully unpacked and MD5 sums checked
package 'covr' successfully unpacked and MD5 sums checked
package 'digest' successfully unpacked and MD5 sums checked
Warning message:
"cannot remove prior installation of package 'digest'"Warning message in file.copy(savedcopy, lib, recursive = TRUE):
"C:\Users\205\.conda\envs\r_study\Lib\R\library\00LOCK\digest\libs\x64\digest.dll를 C:\Users\205\.conda\envs\r_study\Lib\R\library\digest\libs\x64\digest.dll로 복사하는데 문제가 발생했습니다: Permission denied"Warning message:
"restored 'digest'"
package 'DT' successfully unpacked and MD5 sums checked
package 'ellipsis' successfully unpacked and MD5 sums checked
package 'glue' successfully unpacked and MD5 sums checked
package 'git2r' successfully unpacked and MD5 sums checked
package 'httr' successfully unpacked and MD5 sums checked
package 'jsonlite' successfully unpacked and MD5 sums checked
Warning message:
"cannot remove prior installation of package 'jsonlite'"Warning message in file.copy(savedcopy, lib, recursive = TRUE):
"C:\Users\205\.conda\envs\r_study\Lib\R\library\00LOCK\jsonlite\libs\x64\jsonlite.dll를 C:\Users\205\.conda\envs\r_study\Lib\R\library\jsonlite\libs\x64\jsonlite.dll로 복사하는데 문제가 발생했습니다: Permission denied"Warning message:
"restored 'jsonlite'"
package 'memoise' successfully unpacked and MD5 sums checked
package 'rcmdcheck' successfully unpacked and MD5 sums checked
package 'remotes' successfully unpacked and MD5 sums checked
package 'roxygen2' successfully unpacked and MD5 sums checked
package 'rstudioapi' successfully unpacked and MD5 sums checked
package 'rversions' successfully unpacked and MD5 sums checked
package 'sessioninfo' successfully unpacked and MD5 sums checked
package 'devtools' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [5]:
install.packages('dplyr')
also installing the dependencies 'pillar', 'lifecycle', 'tibble', 'tidyselect', 'vctrs'

package 'pillar' successfully unpacked and MD5 sums checked
package 'lifecycle' successfully unpacked and MD5 sums checked
package 'tibble' successfully unpacked and MD5 sums checked
package 'tidyselect' successfully unpacked and MD5 sums checked
package 'vctrs' successfully unpacked and MD5 sums checked
package 'dplyr' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [6]:
install.packages('plyr')
package 'plyr' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [7]:
install.packages('reshape2')
package 'reshape2' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [8]:
install.packages('scales')
also installing the dependency 'farver'

package 'farver' successfully unpacked and MD5 sums checked
package 'scales' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [9]:
install.packages('stringr')
package 'stringr' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [10]:
# 설치 패키지 목록보기 1
library()
In [12]:
# 설치 패키지 목록보기 2
# installed.packages()
In [14]:
# installed.packages()[,c('Package','Version')]

Help

  • R 자체 도움말
In [15]:
help('rep')
In [16]:
?('rep')
In [17]:
help(ggplot2::gplot)
Error in help(ggplot2::gplot): 'topic' should be a name, length-one character vector or reserved word
Traceback:

1. help(ggplot2::gplot)
2. stop("'topic' should be a name, length-one character vector or reserved word")

R에서 외부데이터 읽기

CSV 읽기

  • read.csv('파일명')
In [20]:
List <- read.csv('R-ggagi-data/example_studentlist.csv')
List
namesexagegradeabsencebloodtypeheightweight
김길동남자 23 3 O 165.3 68.2
이미린여자 22 2 AB 170.1 53.0
홍길동남자 24 4 B 175.0 80.1
김철수남자 23 3 AB 182.1 85.7
손세수여자 20 1 A 168.0 49.5
박미희여자 21 2 O 162.0 52.0
강수친여자 22 1 O 155.2 45.3
이희수여자 23 1 A 176.9 55.0
이철린남자 23 3 B 178.5 64.2
방희철남자 22 2 B 176.1 61.3
박수호남자 24 4 O 167.1 62.0
임동민남자 22 2 AB 180.0 75.8
김민수남자 21 1 A 162.2 55.3
이희진여자 23 3 O 176.1 53.1
김미진여자 22 2 B 158.2 45.2
김동수남자 24 4 B 168.6 70.2
여수근남자 21 1 A 169.2 62.2
In [22]:
# header가 없다는 뜻
List <- read.csv('R-ggagi-data/example_studentlist.csv', header = F)  
List
V1V2V3V4V5V6V7V8
name sex age grade absence bloodtypeheight weight
김길동 남자 23 3 O 165.3 68.2
이미린 여자 22 2 AB 170.1 53
홍길동 남자 24 4 B 175 80.1
김철수 남자 23 3 AB 182.1 85.7
손세수 여자 20 1 A 168 49.5
박미희 여자 21 2 O 162 52
강수친 여자 22 1 O 155.2 45.3
이희수 여자 23 1 A 176.9 55
이철린 남자 23 3 B 178.5 64.2
방희철 남자 22 2 B 176.1 61.3
박수호 남자 24 4 O 167.1 62
임동민 남자 22 2 AB 180 75.8
김민수 남자 21 1 A 162.2 55.3
이희진 여자 23 3 O 176.1 53.1
김미진 여자 22 2 B 158.2 45.2
김동수 남자 24 4 B 168.6 70.2
여수근 남자 21 1 A 169.2 62.2

TXT

In [23]:
# read.table() - 구분자 Tab이 기본, header=F 가 기본
List  <- read.table('R-ggagi-data/studentlist.txt')
List
V1V2V3V4V5V6V7
이름 성별 나이 학년 휴학여부혈액형
김길동 남자 23 3 O 165.3
이미린 여자 22 2 AB 170.1
홍길동 남자 24 4 B 175
김철수 남자 23 3 AB 182.1
손세수 여자 20 1 A 168
박미희 여자 21 2 O 162
강수친 여자 22 1 O 155.2
이희수 여자 23 1 A 176.9
이철린 남자 23 3 B 178.5
방희철 남자 22 2 B 176.1
박수호 남자 24 4 O 167.1
임동민 남자 22 2 AB 180
김민수 남자 21 1 A 162.2
이희진 여자 23 3 O 176.1
김미진 여자 22 2 B 158.2
김동수 남자 24 4 B 168.6
여수근 남자 21 1 A 169.2
In [24]:
# 헤더가 있어요
List  <- read.table('R-ggagi-data/studentlist.txt', header = T)
List
이름성별나이학년휴학여부혈액형
김길동남자 23 3 O 165.3
이미린여자 22 2 AB 170.1
홍길동남자 24 4 B 175.0
김철수남자 23 3 AB 182.1
손세수여자 20 1 A 168.0
박미희여자 21 2 O 162.0
강수친여자 22 1 O 155.2
이희수여자 23 1 A 176.9
이철린남자 23 3 B 178.5
방희철남자 22 2 B 176.1
박수호남자 24 4 O 167.1
임동민남자 22 2 AB 180.0
김민수남자 21 1 A 162.2
이희진여자 23 3 O 176.1
김미진여자 22 2 B 158.2
김동수남자 24 4 B 168.6
여수근남자 21 1 A 169.2
In [26]:
List  <- read.table('R-ggagi-data/studentlist2.txt', header = T, sep = ';')
List
이름성별나이학년휴학여부혈액형
김길동남자 23 3 O 165.3
이미린여자 22 2 AB 170.1
홍길동남자 24 4 B 175.0
김철수남자 23 3 AB 182.1
손세수여자 20 1 A 168.0
박미희여자 21 2 O 162.0
강수친여자 22 1 O 155.2
이희수여자 23 1 A 176.9
이철린남자 23 3 B 178.5
방희철남자 22 2 B 176.1
박수호남자 24 4 O 167.1
임동민남자 22 2 AB 180.0
김민수남자 21 1 A 162.2
이희진여자 23 3 O 176.1
김미진여자 22 2 B 158.2
김동수남자 24 4 B 168.6
여수근남자 21 1 A 169.2

Excel 읽기

  • readxl 패키지 사용
In [27]:
install.packages('readxl')
package 'readxl' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [30]:
# 부착시킨다 : 설치 후 라이브러리를 띄운다는 의미
library(readxl)
In [31]:
df <- read_excel(path = 'R-ggagi-data/studentlist.xlsx', sheet = 'Sheet1', 
                 col_names = TRUE)
df
namesexagegradeabsencebloodtypeheightweight
김길동남자 23 3 O 165.3 68.2
이미린여자 22 2 AB 170.1 53.0
홍길동남자 24 4 B 175.0 80.1
김철수남자 23 3 AB 182.1 85.7
손세수여자 20 1 A 168.0 49.5
박미희여자 21 2 O 162.0 52.0
강수친여자 22 1 O 155.2 45.3
이희수여자 23 1 A 176.9 55.0
이철린남자 23 3 B 178.5 64.2
방희철남자 22 2 B 176.1 61.3
박수호남자 24 4 O 167.1 62.0
임동민남자 22 2 AB 180.0 75.8
김민수남자 21 1 A 162.2 55.3
이희진여자 23 3 O 176.1 53.1
김미진여자 22 2 B 158.2 45.2
김동수남자 24 4 B 168.6 70.2
여수근남자 21 1 A 169.2 62.2
In [32]:
df <- read_excel(path = 'R-ggagi-data/studentlist.xlsx', sheet = 'Sheet1', 
                 col_names = FALSE)
df
New names:
* `` -> ...1
* `` -> ...2
* `` -> ...3
* `` -> ...4
* `` -> ...5
* ...
...1...2...3...4...5...6...7...8
name sex age grade absence bloodtype height weight
김길동 남자 23 3 O 165.3 68.2
이미린 여자 22 2 AB 170.1 53
홍길동 남자 24 4 B 175 80.099999999999994
김철수 남자 23 3 AB 182.1 85.7
손세수 여자 20 1 A 168 49.5
박미희 여자 21 2 O 162 52
강수친 여자 22 1 O 155.1999999999999945.3
이희수 여자 23 1 A 176.9 55
이철린 남자 23 3 B 178.5 64.2
방희철 남자 22 2 B 176.1 61.3
박수호 남자 24 4 O 167.1 62
임동민 남자 22 2 AB 180 75.8
김민수 남자 21 1 A 162.1999999999999955.3
이희진 여자 23 3 O 176.1 53.1
김미진 여자 22 2 B 158.1999999999999945.2
김동수 남자 24 4 B 168.6 70.2
여수근 남자 21 1 A 169.2 62.2

SPSS 프로그램 sav 읽기

  • SPSS 통계 프로그램
  • foreign 패키지 사용
In [36]:
install.packages('foreign')
Warning message:
"package 'foreign' is not available (for R version 3.6.1)"
In [38]:
# 부착
library(foreign)
In [39]:
Score <- read.spss('R-ggagi-data/score.sav')
Score
$ID
  1. 1
  2. 2
  3. 3
$Score
  1. 80
  2. 90
  3. 100

XML 읽기

  • XML 패키지 사용
In [40]:
install.packages('XML')
package 'XML' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\205\AppData\Local\Temp\RtmpAjiDQV\downloaded_packages
In [41]:
library('XML')
Warning message:
"package 'XML' was built under R version 3.6.3"
In [42]:
DocFromXML <- xmlTreeParse('R-ggagi-data/score.xml')
DocFromXML
$doc
$file
[1] "R-ggagi-data/score.xml"

$version
[1] "1.0"

$children
$children$mathscore
<mathscore>
 <student>
  <name>Peter</name>
  <score>100</score>
 </student>
 <student>
  <name>Abel</name>
  <score>90</score>
 </student>
 <student>
  <name>Elin</name>
  <score>80</score>
 </student>
</mathscore>


attr(,"class")
[1] "XMLDocumentContent"

$dtd
$external
NULL

$internal
NULL

attr(,"class")
[1] "DTDList"

attr(,"class")
[1] "XMLDocument"         "XMLAbstractDocument"
In [45]:
DocFromXML <- xmlTreeParse('R-ggagi-data/score.xml', useInternalNodes = T)
DocFromXML
<?xml version="1.0"?>
<mathscore>
  <student>
    <name>Peter</name>
    <score>100</score>
  </student>
  <student>
    <name>Abel</name>
    <score>90</score>
  </student>
  <student>
    <name>Elin</name>
    <score>80</score>
  </student>
</mathscore>
 
In [46]:
RootNode <- xmlRoot(DocFromXML)
RootNode
<mathscore>
  <student>
    <name>Peter</name>
    <score>100</score>
  </student>
  <student>
    <name>Abel</name>
    <score>90</score>
  </student>
  <student>
    <name>Elin</name>
    <score>80</score>
  </student>
</mathscore> 
In [49]:
Names <- xpathSApply(RootNode, '//name', xmlValue)  # name 노드의 text값을 뽑아줘
Names
  1. 'Peter'
  2. 'Abel'
  3. 'Elin'
In [50]:
Scores <- xpathSApply(RootNode, '//score', xmlValue)
Scores
  1. '100'
  2. '90'
  3. '80'
In [51]:
# Data 합치기
NameScore = data.frame(NAME=Names, SCORE=Scores)
NameScore
NAMESCORE
Peter100
Abel 90
Elin 80
In [52]:
# Ctrl + C, Ctrl + V 로 가져오기
List <- read.delim('clipboard', header = T)
List
NAMESCORE
Peter100
Abel 90
Elin 80

Tip

Github로 패키지 설치하기

In [53]:
#install.packages('devtools')
In [54]:
library('devtools')
Warning message:
"package 'devtools' was built under R version 3.6.3"Loading required package: usethis
Warning message:
"package 'usethis' was built under R version 3.6.3"
In [56]:
# github.com/yihui/knitr
install.packages('yihui/knitr') # 깃허브 뒤에 주소 입력
Warning message:
"package 'yihui/knitr' is not available (for R version 3.6.1)"

'R' 카테고리의 다른 글

[R] 데이터 개념 이해하기 (2-2)  (0) 2020.07.08
[R] 데이터 개념 이해하기(2)  (0) 2020.07.03
[R] 데이터 개념 이해하기(기초)  (0) 2020.07.02
[R] Python 가상환경 생성 및 R 주피터 노트북 연결  (0) 2020.07.02
R 설치  (0) 2020.07.02

댓글