21.3.26 / R / 강의 수강 및 실습

pjk·2021년 3월 26일
0

[매일코딩 스터디]

목록 보기
45/62

Today

강의

R 프로그램, 통계 12강 까지

스터디 내용

9. 파일 읽기

lungdata <- read.table(file.choose(), header=TRUE) # pd.read_text
head(lungdata) # df.head
tail(lungdata) # df.tail

lungdata2 <- read.csv(file.choose(), header = T) #pd.read_csv
head(lungdata2)
tail(lungdata2)
str(lungdata2)

max(lungdata2Heights)min(lungdata2Heights) min(lungdata2Heights)

10. 주사위 던지기

* uniform distribution simulation

* p =1/n, n of die = 6

roll <- 10000000
n <- 6
dice <- ceiling(runif(roll) * n)
a <- table(die)
barplot(a)

sum of two dice

roll <- 1000
dice <- ceiling(runif(roll) 6) + ceiling(runif(roll) 6) #round()
a <- table(dice)
barplot(a) # plt.bar()

a[7]

11. tidyr, 데이터 정리

install.packages("tidyr") #pip.install
install.packages("dplyr")

library(tidyr) #import
library(dplyr)

member <- data.frame(family = c(1,2,3), namef = c('a','b','c'),
agef = c(30,40,50), namem = c('d','e','f'), agem = c(44,53,25)) # pd.dataframe
member

a <- gather(member, key, value, namef:agem) # 값들을 길게 아래로 뿌려줌, unstack
a
b <- separate(a, key, c('variable', 'type'), -1) # key 값을 comma 뒤 기준으로 하여 두 가지로 쪼갬
b
new <- spread(b, variable, value) # stack

new

new2 <- member %>% # pipeline
gather(key, value, namef:agem) %>%
separate(key, c('variable', 'type'), -1) %>%
spread(variable, value)

new2

filter(new, age >= 30)

select(member, family, namef, agef)

12. character, string

x <- 'what is your name?'
x <- "what's your name?"
x

y <- character()
y
length(y)

y2 <- ""
class(y) # type()
class(y2)
length(y2)

y <- c('e', '12', '2')
y <- character(10)
length(y)
y[3] <- "Third"
y[12] <- "Twelves"
length(y)

y[11] <- "11"
y

n = 3
m = "3"
is.character(n) # 문자형인지 확인
is.character((m))

class(n)
class(m)

as.character(n) # 문자처럼 표현, 실제로 변경은 아님
class(n)

t <- c(1:5)
t2 <- c(1:5, "a")
t2 # 형식이 모두 같아야 하기에 모두 문자형으로 변경됨.
class(t)
class(t2)

t3 <- c(1:4, TRUE, FALSE) # True는 1 False는 0
t3
class(t3)

t4 <- c(1:4, TRUE, FALSE, "a")
class(t4)

df1 <- data.frame(n=c(1:4, "a"), letters=c('a','b','c','23','1'))
df1
str(df1)

Tomorrow

  • 매일 1시간 정도 R 강의 수강 및 실습 진행

Summary

  • R 문법과 pandas 문법이 거의 1:1 대응되어 익숙하고 빠르게 배울 수 있을것 같다.
profile
성장

0개의 댓글