In this article, we will be discussing how to extract data from existing data frames within R.
If you aren’t already familiar with the function of braces( ‘[‘ and ‘]’ ) within R, we will briefly review their usage.
When you encounter braces in R, the variables specified within the braces themselves, are instructing R to query and return data.
[ X , Y ]
Above is an example of how such a query would appear within the R code base.
X - Specifies Row
Y - Specifies Column
So if a programmer were to write the code:
E <- DataFrameA[ 1 , 2, drop = FALSE]
R would interpret this to mean: return the data from Row: 1, Column: 2, and store this data in factor variable ‘E’.
Leaving either the left or the right position empty in a braces related query, instructs R to return ALL data.
Therefore:
E <- DataFrameA[ 1 , , drop = FALSE]
Would instruct R to return ALL Column data from Row:1. (And store this data in ‘E’)
While:
E <- DataFrameA[ , 1 ] Would instruct R to return ALL Row data from Column:1. (And store this data in ‘E’)
The following are examples of code samples which extract data from R Data Frames.
E <- DataFrameA[3, 2, drop = FALSE] Extracts the third element in the second column of DataFrameA, and stores that element in factor variable ‘E'.
E <- DataFrameA[c(1 , 3), 2, drop = FALSE] Extracts the data within row 1 and row 3, within column 2, of DataFrameA. The data is then stored in factor variable ‘E’.
E <- DataFrameA[5, ] Extracts row 5, and all column data contained within row 5. The data will be stored in DataFrame ‘E'.
E <- DataFrameA[ , 8] Extract all rows data from column 8. The data is then stored in factor variable ‘E’.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.