15:13, 9th March 2022
Complete tutorial on using apply functions in R
The apply family of functions in R provides a flexible approach to applying operations across data structures such as data frames, lists and vectors. Each function within this family serves a distinct purpose. For instance, apply is commonly used for row-wise or column-wise operations on matrices or data frames, while lapply and sapply are tailored for list and vector manipulations, respectively. Next, tapply extends this capability by allowing grouping operations based on one or more factors.
These functions are particularly useful when dealing with complex data transformations that require iteration, though they are often less efficient than vectorised operations or dplyr alternatives. The dplyr package offers a more intuitive and readable approach for data manipulation, especially when working with tidy data.
Functions like group_by and summarise in dplyr can replicate the functionality of tapply, but with the added benefit of seamless integration with the pipe operator, enabling a more fluid workflow. For example, grouping data by a variable and calculating summary statistics can be achieved in a single, concise pipeline. Similarly, the across function in dplyr mirrors the row-wise or column-wise operations of apply, but within the context of tidy data principles.
The choice between apply and dplyr depends on the specific task and the structure of the data. It happens that apply functions are well-suited for quick, ad hoc calculations, particularly when working with matrices or lists. On the other hand, dplyr excels in scenarios where data is already in a tidy format and further transformations or analyses are required.
Both approaches have their strengths and the decision often hinges on the complexity of the task, the need for readability and the compatibility with downstream workflows. Understanding the nuances of each method allows for more effective data analysis and manipulation in R.
15:24, 8th March 2022
How to Calculate a Cumulative Average in R
Calculating a cumulative average in R involves dividing the cumulative sum of a dataset by the sequence number of its elements, a process achievable through multiple methods. Using base R, the cumsum function combined with seq_along provides a straightforward approach, while the dplyr package offers a more efficient alternative with its cummean function, particularly beneficial for large datasets.
A sample dataset comprising monthly values demonstrates how each method generates identical results, illustrating the cumulative average as a running mean that updates with each additional data point, such as the average of the first three values being 5 and the first six values averaging 6. This technique is useful for tracking trends over time, with the output clearly displayed in the modified data frame, showing how each row's cumulative average evolves incrementally.
14:35, 7th March 2022
How To Get Twitter Data Using R
Here is a demonstration of how to retrieve Twitter data using R, specifically through the rtweet library, which requires a developer account along with consumer and access keys for API authorisation. Once installed and authorised, the library enables users to search for tweets using keywords or hashtags, stream a live random sample of approximately one percent of all tweets and apply filters to refine results by language, engagement thresholds or by excluding retweets, quotes and replies. Additional functionality includes retrieving user timelines of up to 3,200 posts, identifying the most recently liked tweets from a given account, searching for users associated with particular keywords, accessing follower and friend lists and monitoring trending topics by location using either city names or geographic coordinates.
07:55, 28th February 2022
Good Practices: how to sanitize, validate and escape in PHP
Proper data handling in PHP applications is often neglected even in competitive tech environments, despite being fundamental to building secure and stable software. The core practices involved are sanitising input, validating data and escaping output, each serving a distinct purpose.
Sanitising removes unsafe characters from incoming data before it reaches storage, and PHP offers tools such as htmlentities() with the ENT_QUOTES flag and the filter_var() function for handling common inputs like email addresses, though dedicated libraries such as HTML Purifier offer greater depth. SQL injection risks can be mitigated by using PDO prepared statements rather than embedding user input directly into queries.
Validation, which differs from sanitisation in that it confirms data meets expected criteria rather than cleaning it, can also be handled through filter_var() using flags such as FILTER_VALIDATE_EMAIL, or through more powerful third-party libraries. Finally, escaping output before displaying it to users prevents malicious code from being rendered or executed on a page. Taken together, these practices reduce security vulnerabilities, improve application reliability and represent a professional standard that developers at all levels should apply consistently from the outset of any project.
09:10, 26th February 2022
Show Amount Of Posts, Pages, Categories, Tags, Comments For WordPress Themes
For WordPress versions 2.5 and above, developers can display statistical counts for various content types on the front end of a WordPress theme using a set of built-in functions. The wp_count_posts() function retrieves the number of published posts and pages, while wp_count_terms() handles the counting of categories and tags.
For comments, either get_comment_count() or wp_count_comments() can be used, with both offering the ability to filter by status, such as approved, spam or awaiting moderation. These values can be formatted and displayed as an HTML list, which developers are free to style using CSS classes or IDs to suit their design needs.
09:08, 26th February 2022
Count the number of posts in the custom post type in WordPress
WordPress provides several methods to count posts within custom post types, with WP_Query being a flexible approach that uses parameters such as category_name and post_type to retrieve specific data. This class requires defining these variables in an array, which is then passed to the WP_Query constructor to generate an object that includes the total number of posts found.
Alternatively, wp_count_posts offers a simpler way to count posts based on their status, such as published or drafted, but it does not support filtering by category slugs. The get_posts function can also be used, though it is less efficient for large datasets due to its tendency to load all posts into memory before counting. Each method has distinct use cases, with WP_Query generally preferred for its versatility in handling complex queries involving custom taxonomies and post types.
11:07, 21st February 2022
R Error: Can’t rename columns that don’t exist.
When using both the plyr and dplyr packages in R, a conflict can arise with the rename function, as both packages contain a function of the same name. Because R defaults to the most recently loaded package, using the rename function after loading dplyr last will trigger the error "Can't rename columns that don't exist" if the code was written with the plyr version in mind. The fix is straightforward: by explicitly specifying the package name before the function using the format plyr::rename, R is directed to use the correct version, allowing column renaming to proceed without error.
11:06, 21st February 2022
A common belief among R programmers is that loops are slow and should always be replaced with vectorised calculations or apply family functions such as sapply. Benchmarking tests comparing vectorised calculations, naive loops and vapply across two tasks, calculating square roots and formatting numbers with paste, reveal a more nuanced picture.
Vectorised calculations remain the fastest option for simple mathematical operations, running more than ten times faster than a loop, but vapply performs notably worse than a naive loop in that same scenario. For more complex functions like paste, the performance gap narrows considerably, with loops taking only around 50% longer than the vectorised approach and vapply performing at roughly the same speed as a loop.
The conclusion drawn is that vectorisation should be used where available, but choosing apply-family functions over loops for the sake of speed alone is not well justified. Instead, the preference for apply family functions is better understood as a matter of coding style and readability rather than performance.
18:52, 18th February 2022
SS64 is a reference guide created by Simon Sheppard that offers syntax examples and documentation for commonly used database and operating system commands. It covers areas including SQL Server and MySQL, and is regularly updated, with recent additions including PowerShell colour options and chmod references.
18:51, 18th February 2022
Gunicorn
A Python-based WSGI HTTP server designed for deploying web applications, this tool is known for its reliability in production environments and ease of use, requiring minimal configuration. It supports multiple frameworks including Django, Flask and FastAPI and offers various worker types to handle different workloads, from synchronous processing to asynchronous handling of I/O-bound tasks. The software is compatible with both WSGI and ASGI standards, allowing integration with a range of application stacks without modification. It provides documentation for deployment options such as Nginx, systemd and Docker, along with community support and resources for troubleshooting and contribution.