R For Dummies by Joris Meys
Author:Joris Meys
Language: eng
Format: epub
Publisher: Wiley
Published: 2012-06-01T04:00:00+00:00
VAT <- sapply(client, switch, private=1.12, public=1.06, 1)
tot.price <- net.price * VAT
round(tot.price)
}
Applying a function to a data frame
You also can use sapply() on lists and data frames. In this case, sapply() applies the specified function on every element in that list. Because data frames are lists as well, everything in this section applies to both lists and data frames.
Imagine that you want to know which type of variables you have in your data frame clients. For a vector, you can use the class() function to find out the type. In order to know this for all variables of the data frame at once, you simply apply the class() function to every variable by using sapply() like this:
> sapply(clients,class)
hours public type
“numeric” “logical” “character”
R returns a named vector that gives you the types of every variable, and it uses the names of the variables as names for the vector. In case you use a named list, R uses the names of the list elements as names for the vector.
Simplifying results (or not) with sapply
The sapply() function doesn’t always return a vector. In fact, the standard output of sapply is a list, but that list gets simplified to either a matrix or a vector if possible.
If the result of the applied function on every element of the list or vector is a single number, sapply() simplifies the result to a vector.
If the result of the applied function on every element of the list or vector is a vector with exactly the same length, sapply() simplifies the result to a matrix.
In all other cases, sapply() returns a (named) list with the results.
Say you want to know the unique values of every variable in the data frame clients. To get all unique values in a vector, you use the unique() function. You can get the result you want by applying that function to the data frame clients like this:
> sapply(clients, unique)
$hours
[1] 25 110 125 40
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26593)
The Fifty Shades Trilogy & Grey by E L James(19079)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19058)
Shot Through the Heart by Mercy Celeste(18936)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17112)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16994)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16878)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16825)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16447)
The Subtle Art of Not Giving a F*ck by Mark Manson(14353)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14141)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13646)
Scorched Earth by Nick Kyme(12771)
Drei Generationen auf dem Jakobsweg by Stein Pia(10965)
Suna by Ziefle Pia(10889)
Scythe by Neal Shusterman(10338)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(9562)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(9535)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9522)
