Spread R

Spread R



πŸ”ž ALL INFORMATION CLICK HERE πŸ‘ˆπŸ»πŸ‘ˆπŸ»πŸ‘ˆπŸ»

































Spread R









About Course






Getting Started






Getting Data






Tidy Data






Graphics









The first tidyr function we will look into is the spread() function. With spread() it does similar to what you would expect. We have a data frame where some of the rows contain information that is really a variable name. This means the columns are a combination of variable names as well as some data. The picture below displays this:
We can consider the following data which is table 2:
Notice that in the column of key , instead of there being values we see the following variable names:
In order to use this data we need to have it so the data frame looks like this instead:
Now we can see that we have all the columns representing the variables we are interested in and each of the rows is now a complete observation.
In order to do this we need to learn about the spread() function:
If we consider piping , we can write this as:
Now if we consider table2 , we can see that we have:
Now this table was made for this example so key is the key in our spread() function and value is the value in our spread() function. We can fix this with the following code:
We can now see that we have a variable named cases and a variable named population . This is much most tidy.
We first will load tidyverse. If you have not installed it run the following code:
In this example we will use the dataset population that is part of tidyverse. Print this data:
You should see the table that we have above, now We have a variable named year , assume that we wish to actually have each year as its own variable. Using the spread() function, redo this data so that each year is a variable. Your data will look like this at the end:

r - spread () function in tidyr - Stack Overflow
R Basics | Spread
Spread a key-value pair across multiple columns β€” spread β€’ tidyr
spread : Spread a key-value pair across multiple columns in tidyr: Tidy...
Data Tidying Β· Data Science with R | 2.2.2 spread ()



Tidy data


Reference



Articles





Pivoting


Rectangling


Nested data



More...





News




Releases

Version 1.1.0


Version 1.0.0


Version 0.8.0


Version 0.7.0


Version 0.6.0


Version 0.5.0


Version 0.5.0


Version 0.4.0


Version 0.2.0


Version 0.1.0



Changelog












library ( dplyr )
stocks <- data.frame (
time = as.Date ( '2009-01-01' ) + 0 : 9 ,
X = rnorm ( 10 , 0 , 1 ),
Y = rnorm ( 10 , 0 , 2 ),
Z = rnorm ( 10 , 0 , 4 )
)
stocksm <- stocks %>% gather ( stock , price , - time )
stocksm %>% spread ( stock , price )
#> time X Y Z
#> 1 2009-01-01 -2.0524039 -1.4041640 0.001920052
#> 2 2009-01-02 0.1507482 1.9466316 3.020500225
#> 3 2009-01-03 -0.2928727 -0.1536353 1.369614042
#> 4 2009-01-04 0.2549976 1.7858498 0.673891297
#> 5 2009-01-05 -0.5532382 -1.5550062 5.588266035
#> 6 2009-01-06 1.4051089 0.8735942 -2.716381729
#> 7 2009-01-07 -0.7954609 0.8268879 2.950517792
#> 8 2009-01-08 -1.5665145 1.9526835 -3.442897102
#> 9 2009-01-09 -1.0405791 2.2930010 1.684921683
#> 10 2009-01-10 1.0199337 2.4345434 5.802172853
stocksm %>% spread ( time , price )
#> stock 2009-01-01 2009-01-02 2009-01-03 2009-01-04 2009-01-05 2009-01-06
#> 1 X -2.052403887 0.1507482 -0.2928727 0.2549976 -0.5532382 1.4051089
#> 2 Y -1.404163956 1.9466316 -0.1536353 1.7858498 -1.5550062 0.8735942
#> 3 Z 0.001920052 3.0205002 1.3696140 0.6738913 5.5882660 -2.7163817
#> 2009-01-07 2009-01-08 2009-01-09 2009-01-10
#> 1 -0.7954609 -1.566514 -1.040579 1.019934
#> 2 0.8268879 1.952684 2.293001 2.434543
#> 3 2.9505178 -3.442897 1.684922 5.802173

# Spread and gather are complements
df <- data.frame ( x = c ( "a" , "b" ), y = c ( 3 , 4 ), z = c ( 5 , 6 ))
df %>% spread ( x , y ) %>% gather ( "x" , "y" , a : b , na.rm = TRUE )

# Use 'convert = TRUE' to produce variables of mixed type
df <- data.frame ( row = rep ( c ( 1 , 51 ), each = 3 ),
var = c ( "Sepal.Length" , "Species" , "Species_num" ),
value = c ( 5.1 , "setosa" , 1 , 7.0 , "versicolor" , 2 ))
df %>% spread ( var , value ) %>% str
#> 'data.frame': 2 obs. of 4 variables:
#> $ row : num 1 51
#> $ Sepal.Length: chr "5.1" "7"
#> $ Species : chr "setosa" "versicolor"
#> $ Species_num : chr "1" "2"
df %>% spread ( var , value , convert = TRUE ) %>% str
#> 'data.frame': 2 obs. of 4 variables:
#> $ row : num 1 51
#> $ Sepal.Length: num 5.1 7
#> $ Species : chr "setosa" "versicolor"
#> $ Species_num : int 1 2
Development on spread() is complete, and for new code we recommend
switching to pivot_wider() , which is easier to use, more featureful, and
still under active development.
df %>% spread(key, value) is equivalent to
df %>% pivot_wider(names_from = key, values_from = value)
See more details in vignette("pivot") .
Column names or positions. This is passed to
tidyselect::vars_pull() .
These arguments are passed by expression and support
quasiquotation (you can unquote column
names or column positions).
If set, missing values will be replaced with this value. Note
that there are two types of missingness in the input: explicit missing
values (i.e. NA ), and implicit missings, rows that simply aren't
present. Both types of missing value will be replaced by fill .
If TRUE , type.convert() with asis =
TRUE will be run on each of the new columns. This is useful if the value
column was a mix of variables that was coerced to a string. If the class of
the value column was factor or date, note that will not be true of the new
columns that are produced, which are coerced to character before type
conversion.
If FALSE , will keep factor levels that don't appear in the
data, filling in missing combinations with fill .
If NULL , the column names will be taken from the values of
key variable. If non- NULL , the column names will be given
by "" .
tidyr is a part of the tidyverse , an ecosystem of packages designed with common APIs and a shared philosophy. Learn more at tidyverse.org .

Developed by Hadley Wickham .
Site built by pkgdown .


Voyeur Public Sex Amateur Video
Sex Machine Double Penetration
Facesitting Pee
Sex Pee Girl
Softcore Porn 1080

Report Page