Skip to Content
R Cookbook
book

R Cookbook

by JD Long, Paul Titer, Jee -Won Lee
May 2022
Beginner to intermediate
667 pages
16h 17m
Korean
Insight
Content preview from R Cookbook
Cookbook
559
15.2 루프 반복하기
15.2
󺶷󼧓󽴔󻃧󻇄󼨧󺋿
󻀇󻳫
벡터 또는 리스트의 원소에 대해 코드를 반복하고 싶다.
󼩃󺅿󼄔
보편적으로 사용하는 반복 방법은
for
구조다.
v
가 벡터 또는 리스트일 때,
for
루프
v
의 원소를 하나씩 선택하여
x
에 해당 원소를 대입하고, 거기에 무엇인가를 한다
고 보면 된다.
for (x in v) {
# x
󺴫
󻀃󻪖󻱇󺃏󺹋
󼨷
}
󺙋󻰧
C
와 파이썬을 사용하는 프로그래머들은
for
루프에 대해 알고 있을 것이다.
R
에서는
자주 사용하지 않지만 그래도 유용할 때가 있다.
예를 들면, 다음의
for
루프는 첫 다섯 개의 정수와 그것의 제곱을 출력해 주는 코
드다.
x
를 연속해서
1
,
2
,
3
,
4
,
5
로 설정한 다음 루프의 본문을 각각 실행해 준다.
for (x in 1:5) {
cat(x, x^2, "\n")
}
#> 1 1
#> 2 4
#> 3 9
#> 4 16
#> 5 25
벡터 또는 리스트의 첨자(
subscript
)
1
에 대해서도 반목문을 실행할 수 있는데, 해당
위치의 데이터를 업데이트하는 데 사용하기 편리하다. 다음 코드에서는
v
를 벡터
1:5
로 만들고, 각각을 제곱하여 업데이트해 보도록 하겠다.
v <- 1:5
for (i in 1:5) {
v[[i]] <- v[[i]] ^ 2
}
1
(옮긴이) 벡터 등에서 특정 원소를 추출한 것
R Cookbook ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

R Cookbook

R Cookbook

Paul Teetor
R Graphics Cookbook

R Graphics Cookbook

Winston Chang
R Cookbook, 2nd Edition

R Cookbook, 2nd Edition

JD Long, Paul Teetor

Publisher Resources

ISBN: 9788966262984