Está en la página 1de 3

notas de R

l <- c(10:20)
> l[(l<15)]
[1] 10 11 12 13 14
l<15
[1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
> l[(TRUE)]
[1] 10 11 12 13 14 15 16 17 18 19 20
>
l[c(TRUE , TRUE, TRUE , TRUE , TRUE ,FALSE, FALSE ,FALSE, FALSE, FALSE, FALSE)]
[1] 10 11 12 13 14

---------- > data.frame(a,b,c,d,stringsAsFactors = TRUE)

------------> factor_day <- factor(day_vector, order = TRUE, levels =c('morning',


'midday', 'afternoon', 'evening', 'midnight'))

------matrix(data, nrow, ncol, byrow = FALSE)

----> data.frame(a,b,c,d,stringsAsFactors = TRUE)

-------------------names(df) <- c('ID', 'items', 'store', 'price')


> df
ID items store price
1 10 book TRUE 2.5
2 20 pen FALSE 8.0
3 30 textbook TRUE 10.0
4 40 pencil_case FALSE 7.0
-----------------> str(df)
'data.frame': 4 obs. of 4 variables:
$ ID : num 10 20 30 40
$ items: Factor w/ 4 levels "book","pen","pencil_case",..: 1 2 4 3
$ store: logi TRUE FALSE TRUE FALSE
$ price: num 2.5 8 10 7
---
df[1,2] ****
[1] book
Levels: book pen pencil_case textbook
> class(df[1,2])
[1] "factor"

df[, c('ID', 'store')]


> df[1,c('items')]
[1] book
Levels: book pen pencil_case textbook

df[,'ID']
[1] 10 20 30 40
> df$ID
[1] 10 20 30 40
> df[,1]
[1] 10 20 30 40

-----
# Create a new vector
quantity <- c(10, 35, 40, 5)

# Add `quantity` to the `df` data frame


df$quantity <- quantity

-----------

> v <- 1:4; class(v)


[1] "integer"
> v <- c(1,2,3,4);class(v)
[1] "numeric"

------

df <-data_frame[order(-data_frame$c3, data_frame$c4),]

--------
print(df_primary)
# A tibble: 5 x 2
ID y
<chr> <dbl>
1 A 5
2 B 5
3 C 8
4 D 0
5 F 9

print(df_secondary)
# A tibble: 5 x 2
ID y
<chr> <dbl>
1 A 30
2 B 21
3 C 22
4 D 25
5 E 29

left_join(df_primary, df_secondary, by ='ID')


# A tibble: 5 x 3
ID y.x y.y
<chr> <dbl> <dbl>
1 A 5 30
2 B 5 21
3 C 8 22
4 D 0 25
5 F 9 NA

--------

messy <- data.frame("A(i)"= c("A1", "A2", "A3"),B1 = c("A1.B1","A2.B1","A3.B1"),B2


= c("A1.B2","A2.B2","A3.B2"),B3 = c("A1.B3","A2.B2","A3.B3"),B4 =
c("A1.B4","A2.B4","A3.B4"))
messy %>% gather(L1, L2, B1:B4)
print(messy)
A.i. B1 B2 B3 B4
1 A1 A1.B1 A1.B2 A1.B3 A1.B4
2 A2 A2.B1 A2.B2 A2.B2 A2.B4
3 A3 A3.B1 A3.B2 A3.B3 A3.B4
> messy %>% gather("L1(Bj)", "L2 (Ai.Bj)", B1:B4)
A.i. L1(Bj) L2 (Ai.Bj)
1 A1 B1 A1.B1
2 A2 B1 A2.B1
3 A3 B1 A3.B1
4 A1 B2 A1.B2
5 A2 B2 A2.B2
6 A3 B2 A3.B2
7 A1 B3 A1.B3
8 A2 B3 A2.B2
9 A3 B3 A3.B3
10 A1 B4 A1.B4
11 A2 B4 A2.B4
12 A3 B4 A3.B4

messy %>% gather("L1(Bj)", "L2 (Ai.Bj)", B1:B4) %>% spread("L1(Bj)", "L2 (Ai.Bj)")
A.i. B1 B2 B3 B4
1 A1 A1.B1 A1.B2 A1.B3 A1.B4
2 A2 A2.B1 A2.B2 A2.B2 A2.B4
3 A3 A3.B1 A3.B2 A3.B3 A3.B4

--
colnames(movies)[colnames(movies) == 'surname'] <- 'name'
----------

cars <- mutate(cars, weight_class = as.factor(cut(cars$Weight, breaks =


Weight_class$brks,include.lowest = TRUE,labels = FALSE)))

También podría gustarte