1. 데이터 입력
- keyboard : scan()
n <- scan()
ss <- scan(what ="") # 문자열 입력
df1 <- data.frame()
df1 <- edit(df1) # data frame 수정창 출력
2 파일 읽기
getwd() # 작업경로 확인. setwd()
list.dirs() # 작업경로의 폴더 정보
list.files() # 작업경로의 파일 정보
stud <- read.table(file = "testdata/student1.txt", header = TRUE) #테이블 read. header(defalut : FALSE)
stud1 <- read.table(file = "testdata/student2.txt", header = TRUE, sep =";") #sep : 구분자
stud2 <- read.table(file = "testdata/student3.txt", header = TRUE, sep =" ", na.strings = "-") # na.strings : 해당 값은 NA로 처리
stud3 <- read.csv(file = "testdata/student4.txt", header = TRUE, na.strings = "-") # read.csv : csv 파일 읽기 (csv 구분자 , 로 된 파일)
class(stud3) # data.frame
datas <- read.csv("https://raw.githubusercontent.com/pykwon/Test-datas-for-R/master/agedata.csv") # 웹 data 가져오기
3 출력
- console
x <- 10; y <- 20; z <- x*y
cat(x,y,z)
cat("결과는", z)
print(x)
print(x,y) # 1개만 출력에 참여
4 자료 저장
sink("output/my.txt") # 저장 선언
datas <- read.csv("https://raw.githubusercontent.com/pykwon/Test-datas-for-R/master/agedata.csv")
head(datas,5)
kbs <- 9
kbs
sink() # 저장 작업 종료
#DataFrame 저장
name <- c("관우","장비", "유비")
age <- c(35,33,20)
gender <- c("M","M","F")
myframe <- data.frame(name, age, gender)
myframe
# name age gender
# 1 관우 35 M
# 2 장비 33 M
# 3 유비 20 F
write.table(myframe, "output/my1.txt") #파일저장
write.table(myframe, "output/my2.txt", fileEncoding ="utf-8")
read.table("output/my1.txt") #파일 불러오기
'BACK END > R' 카테고리의 다른 글
[R] R 정리 6 - 조건문, 반복문 (0) | 2021.01.28 |
---|---|
[R] R 정리 5 - 도수 분포표 (0) | 2021.01.28 |
[R] R 정리 3 - 자료구조 (0) | 2021.01.27 |
[R] R 정리2 - 변수 (0) | 2021.01.26 |
[R] R 정리1 - 환경구축 (0) | 2021.01.26 |