Package stringr: String SplitSeptember 27, 2016Sometimes it is necessary to take apart a string. The str_split() function in the stringr package allows you to do that.
Let’s use the example dataset Loblolly in r
Here in the first column we have the height of the trees in ft. Lets say that we want to convert this to two columns one for feet and another for inches. (Note: There are quite a few ways to do this, but here we use this method to show off the str_split() function.)
0 Comments
Sorting Entries in a Data Frame by Column (order {base})Megan KressSeptember 28, 2016
extract FunctionThe order function sorts data in ascending order; however, sorting entries in a data frame is not as simple as calling the order function on the data frame. The function order reutrns a permentation, which rearranges the first argument. The first argument should be a vector, but if more than one vector is entered, the following vectors will be used as “tie-breakers.” For more information on the order function, see the CRAN page for the base package. For this example, I used a data set from the MASS package called ChickWeight.
Preparation
Order by Weight OnlyWe will now use the order function to sort the data set by the chicks’ weights.
Order by Weight Then TimeUsing the property of the function order that it takes multiple vector arguments, we will add a second sorting parameter (time).
We can see from the results that adding Time as a sorting parameter changed the ordering of the chicks. Chick #26 (first column name reference) moved from the second sorted row to the ninth sorted row because its time was 2 whereas all other chicks with weight 39 had times of 0.
Why Sort?Sorting data frames can be important if you are trying to run algorithms involving search or lookup or if you are trying to merge two data frames efficiently. Many functions in R that perform these algorithms will perform the sort operation within their function. Manipulating Strings: Paste FunctionSeptember 27, 2016Strings are sets of character data. You may find the need to manipulate strings in one way or another. Here we cover the paste() function. The paste() function takes a set of character arguments and puts them together into one. (Don’t forget you can also always enter help(paste) to find more information in the R help files.)
Here we see only two arguments, but you can have as many as you would like. Also note how there are no spaces in the arguments, but there is in the output. This is because the paste function has a sep argument that determines what to put in between entries, so lets try entering in a seperator.
Note: The seperator can be whatever you would like to define it as. Paste also has the functionality of using lists to pair elements of the lists together:
|
Spatial Ecology @ MSUClick on "Category" below to search for R code compiled by the Zarnetske Spatial & Community Ecology Lab and students in MSU's Spatial Ecology graduate course (FOR870/FW870) Category
All
Archive
October 2016
|