frmeOne <- data.frame(name = character(), color = character(), weight = numeric(),
stringsAsFactors = FALSE)
frmeOne[nrow( frmeOne ) + 1,] <- list("Ladybug", "black", 9)
frmeOne[nrow( frmeOne ) + 1,] <- list("Butterfly", "white", 10)
frmeOne[nrow( frmeOne ) + 1,] <- list("Bumblebee", "black", 7.5)
frmeOne
## name color weight
## 1 Ladybug black 9.0
## 2 Butterfly white 10.0
## 3 Bumblebee black 7.5
frmeTwo <- data.frame(
name = c("Ladybug", "Butterfly", "Bumblebee"),
color = c("black", "white", "black"),
weight = c(9, 10, 7.5),
stringsAsFactors = FALSE)
frmeTwo
## name color weight
## 1 Ladybug black 9.0
## 2 Butterfly white 10.0
## 3 Bumblebee black 7.5
rownames( frmeOne )
## [1] "1" "2" "3"
rownames( frmeOne ) <- frmeOne[["name"]]
frmeOne <- frmeOne[,!( names( frmeOne ) %in% c("name") )]
frmeOne
## color weight
## Ladybug black 9.0
## Butterfly white 10.0
## Bumblebee black 7.5
rownames( frmeOne )
## [1] "Ladybug" "Butterfly" "Bumblebee"
frmeOne["Ladybug",]
## color weight
## Ladybug black 9
frmeOne[1,]
## color weight
## Ladybug black 9
frmeOne["Butterfly",]
## color weight
## Butterfly white 10
frmeOne[2,]
## color weight
## Butterfly white 10
frmeOne["color"]
## color
## Ladybug black
## Butterfly white
## Bumblebee black
frmeOne["color"]["Butterfly",]
## [1] "white"
frmeOne["Butterfly",][["color"]]
## [1] "white"
frmeOne["Butterfly", "color"]
## [1] "white"
frmeOne[["Butterfly", "color"]]
## [1] "white"
frmeOne[2:3,]
## color weight
## Butterfly white 10.0
## Bumblebee black 7.5
frmeOne[c(FALSE, TRUE, TRUE),]
## color weight
## Butterfly white 10.0
## Bumblebee black 7.5
frmeOne[frmeOne[["color"]] == "black",]
## color weight
## Ladybug black 9.0
## Bumblebee black 7.5
frmeOne[["personality"]] <- c("goofy", "shy", "playful")
frmeOne
## color weight personality
## Ladybug black 9.0 goofy
## Butterfly white 10.0 shy
## Bumblebee black 7.5 playful
frmeOne["Boy",] <- list("black", 16, "goofy")
frmeOne
## color weight personality
## Ladybug black 9.0 goofy
## Butterfly white 10.0 shy
## Bumblebee black 7.5 playful
## Boy black 16.0 goofy
frmeOne <- rbind( frmeOne, Girl = list("white", 15, "shy") )
frmeOne
## color weight personality
## Ladybug black 9.0 goofy
## Butterfly white 10.0 shy
## Bumblebee black 7.5 playful
## Boy black 16.0 goofy
## Girl white 15.0 shy
colnames( frmeOne )
## [1] "color" "weight" "personality"
rownames( frmeOne )
## [1] "Ladybug" "Butterfly" "Bumblebee" "Boy" "Girl"
colnames( frmeOne )[[2]]
## [1] "weight"
rownames( frmeOne )[[3]]
## [1] "Bumblebee"
nrow( frmeOne )
## [1] 5
length( rownames( frmeOne ) )
## [1] 5
ncol( frmeOne )
## [1] 3
frmeOne[frmeOne[["color"]] == "black",]
## color weight personality
## Ladybug black 9.0 goofy
## Bumblebee black 7.5 playful
## Boy black 16.0 goofy
frmeOne[( frmeOne[["color"]] == "black" ) & ( frmeOne[["personality"]] == "goofy" ),]
## color weight personality
## Ladybug black 9 goofy
## Boy black 16 goofy
frmeOne[( frmeOne[["color"]] == "black" ) & ( frmeOne[["personality"]] == "goofy" ) &
( frmeOne[["weight"]] > 10 ),]
## color weight personality
## Boy black 16 goofy
frmeOne
## color weight personality
## Ladybug black 9.0 goofy
## Butterfly white 10.0 shy
## Bumblebee black 7.5 playful
## Boy black 16.0 goofy
## Girl white 15.0 shy
frmeThree <- read.delim( "cats.tsv" )
frmeThree
## name color weight personality
## 1 Ladybug black 9.0 goofy
## 2 Butterfly white 10.0 shy
## 3 Bumblebee black 7.5 playful
## 4 Boy black 16.0 goofy
## 5 Girl white 15.0 shy
frmeThree <- read.delim( "cats.tsv", row.names = 1 )
frmeThree
## color weight personality
## Ladybug black 9.0 goofy
## Butterfly white 10.0 shy
## Bumblebee black 7.5 playful
## Boy black 16.0 goofy
## Girl white 15.0 shy
frmeThree <- read.delim( "cats.tsv", row.names = 1, stringsAsFactors = FALSE )
frmeThree
## color weight personality
## Ladybug black 9.0 goofy
## Butterfly white 10.0 shy
## Bumblebee black 7.5 playful
## Boy black 16.0 goofy
## Girl white 15.0 shy
frmeThree[( frmeThree[["color"]] == "black" ) & ( frmeThree[["personality"]] == "goofy" ) &
( frmeThree[["weight"]] > 10 ),]
## color weight personality
## Boy black 16 goofy
square <- function( iNum ) {
iRet <- iNum * iNum
return( iRet )
}
square( -5 )
## [1] 25
add1 <- function( iN ) { return( iN + 1 ) }
sapply( c(1, 2, 3), add1 )
## [1] 2 3 4
sapply( c(1, 2, 3), function( iN ) { iN + 1 } )
## [1] 2 3 4
lapply( list(1, 2, 3), function( iN ) { iN + 1 } )
## [[1]]
## [1] 2
##
## [[2]]
## [1] 3
##
## [[3]]
## [1] 4
frmeOne
## color weight personality
## Ladybug black 9.0 goofy
## Butterfly white 10.0 shy
## Bumblebee black 7.5 playful
## Boy black 16.0 goofy
## Girl white 15.0 shy
catter <- function( lRow ) {
if( lRow[["color"]] == "black" ) {
return( lRow[["weight"]] )
} else {
return( lRow[["color"]] )
}
}
apply( frmeOne, 1, catter )
## Ladybug Butterfly Bumblebee Boy Girl
## " 9.0" "white" " 7.5" "16.0" "white"
apply( frmeOne, 2, function( aCol ) { sample( aCol, 1 ) } )
## color weight personality
## "white" "10.0" "shy"
adNorm <- rnorm( 25 )
adNorm
## [1] -0.3280727 0.1915822 -0.9207494 -0.3977926 0.6358889 0.2427530
## [7] -0.8264071 -2.2081408 -1.0477654 0.3115680 -0.7090009 0.1907443
## [13] -0.1868401 1.1856502 -0.6331794 -0.2002341 -0.3226024 0.4029752
## [19] 1.0334747 0.2558921 -0.6494631 0.1748569 0.8350601 -0.1902398
## [25] 0.8335593
adUnif <- runif( 25 )
adUnif
## [1] 0.68574338 0.66249621 0.23693167 0.04769571 0.05834499 0.51512328
## [7] 0.46649069 0.01811746 0.69780150 0.21673293 0.17516286 0.29036937
## [13] 0.46241285 0.56003750 0.04213301 0.74871698 0.88879987 0.42964628
## [19] 0.85694694 0.29876781 0.57314493 0.32543641 0.88811740 0.17409308
## [25] 0.70327590
t.test( adNorm, adUnif )
##
## Welch Two Sample t-test
##
## data: adNorm and adUnif
## t = -3.2976, df = 30.395, p-value = 0.00249
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.8644746 -0.2034471
## sample estimates:
## mean of x mean of y
## -0.09305931 0.44090156
t.test( adNorm, rnorm( length( adNorm ) ) )
##
## Welch Two Sample t-test
##
## data: adNorm and rnorm(length(adNorm))
## t = 0.14081, df = 40.26, p-value = 0.8887
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.5390661 0.6198220
## sample estimates:
## mean of x mean of y
## -0.09305931 -0.13343728
adX <- rnorm( 40 )
adY <- rnorm( length( adX ) )
adX
## [1] -0.14994545 0.67545519 -0.03161501 -1.29471147 0.03988258 0.88786537
## [7] -0.61859643 2.04069552 -0.48177606 -1.41032173 -1.87142846 -1.21623397
## [13] -0.75419406 0.23803202 -0.35627898 -0.67458184 1.44786791 0.80953937
## [19] -0.58917564 2.04965831 0.11990442 -1.29744088 -0.49114134 -0.09952204
## [25] 1.20401452 0.27946263 0.80173493 0.11011252 0.46582451 0.63337011
## [31] -0.32363017 0.40044303 -0.01421974 0.02379129 -0.42484515 -0.30900940
## [37] -0.73028092 0.33092140 0.50646931 -0.50375692
par( mfrow = c(2, 2) )
hist( adX )
plot( adX, adY )
plot( adX, adY, type = "l" )
barplot( adX )
library(ggplot2)
ggplot( ) + geom_histogram( aes( adX ) )
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot( ) + geom_histogram( aes( adX ) ) + theme_light( )
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot( ) + geom_point( aes( adX, adY ) )
ggplot( ) + geom_line( aes( adX, adY ) )
ggplot( ) + geom_bin2d( aes( adX, adY ) )