Why is dot notation used when instantiating an R variable name? [duplicate] - r

This question already has answers here:
What does the dot mean in R – personal preference, naming convention or more?
(3 answers)
Closed 1 year ago.
I came across a piece of code from https://rpubs.com/boyerag/297592
On one line they have the following code:
ndvi.array <- ncvar_get(nc_data, "NDVI")
I do not understand what is the significance of ndvi.array. Is this some special way of creating an array in R? I assumed arrays had to be created using the array function. Since data in netCDF format and essentially each variable is an array based on the dimensions, is this an elaborate way of naming the identifier or does ndvi have some association with a parameter 'array'

I am new to R, I didn't realize dots don't mean anything. I assumed it was some weird data structure I didn't know. So from this example, it just creates an R variable named "ndvi.array".

Related

How to print the number of data entries inside a variable in R? [duplicate]

This question already has answers here:
How to know a dimension of matrix or vector in R?
(6 answers)
Closed 3 years ago.
I know this is probably a very simple question but I can't seem to find the answer anywhere online. I am trying to print just the number of data points inside of a variable that I created but I can't figure out how.
I tried using summary() or num() or n() but I am really just making stuff up here and cannot seem to figure it out at all.
For my specific example I have a data set on peoples heights, age, weight, gender, stuff like that. I used
one_sd_weight <- cdc$weight[abs(cdc$weight - mean(cdc$weight)) <= sd(cdc$weight)]
to determine how many of the weights fall within one standard deviation of the mean. After I do this, I can see that on the right side it created a new variable called one_sd_weight that contains 14152 out of the original 20000 entries. How do I print the number 14152 as a variable? For the work I am doing I need to create a new variable that just contains one number, 14152 or whatever number is produced when I run the code above. For example, I need to create
n_one_sd <- 14152
without typing in 14152, instead typing some function that grabs the number of entries in one_sd_weight.
I have tried things like summary() and n() but only receive error messages in return. Any help is greatly appreciated!!
n_one_sd <- length(one_sd_weight)
You're looking for length (in case of a vector) or nrow in case of a matrix/data.frame.
Or you can use NROW() for both, that should work too.

When can I use brackets and when do I use the $-sign? [duplicate]

This question already has answers here:
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
(11 answers)
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 3 years ago.
I'm doing what seems to be a simple exercise. I'm supposed to create a histogram of the ages on the passengers on the titanic. The data frame "titanic" is already spacified. Since age is the sixth variable (I checked) I write the following:
hist(titanic[6])
This does not seem to work for some reason. R generates an error message and tells med that "'x' must be numeric".
(The variable age is indeed numeric, I checked this both with the str()-function and by executing the titanic[6]-command outside of the function.)
Meanwhile I can write it like this without any problems:
hist(titanic$Age)
Why can I use one and not the other? When, in general, can I use brackets and when do I have to use the $-sign?

Column names: () being replaced by dots when reading csv files [duplicate]

This question already has answers here:
data.frame without ruining column names
(2 answers)
Closed 9 years ago.
I have a CSV file containing some columns with names like "beauty & spas", "american (new)" etc. When I read this file in R and use names() to see column names, they have been converted to "beauty...spas.1" and "american...new..1". How do I prevent them from being converted? I do not want to correct them manually.
If you read the documentation carefully at ?read.table (or ?read.csv) you will quickly see that there is an argument called check.names. You most likely want to set that to FALSE. Keep in mind, though, that those are not syntactically valid column names in R, so you you might actually prefer to change them to something that R will handle more smoothly anyway.

How to list the identifiers that `library(some.package)` would add to current environment? [duplicate]

This question already has answers here:
find all functions (including private) in a package
(2 answers)
Closed 6 years ago.
How can I list all the identifiers (function names, variable names, etc.) that would be added1 to my current environment if I were to run
library(some.package)
I want to do this without actually adding all those identifiers to my current environment.
1 Point of pedantry: I use the verb "added" somewhat loosely here, to refer to not only those names from some.package that would be entirely new to the current environment, but also those that would shadow names already existing in the current environment.
Are you perhaps looking for this?
getNamespaceExports("stringr")
EDIT:
For data you could do something like
data(package = 'ggplot2')[['results']][, 'Item']

How to read column names 'as is' from CSV file? [duplicate]

This question already has answers here:
data.frame without ruining column names
(2 answers)
Closed 9 years ago.
I have a CSV file containing some columns with names like "beauty & spas", "american (new)" etc. When I read this file in R and use names() to see column names, they have been converted to "beauty...spas.1" and "american...new..1". How do I prevent them from being converted? I do not want to correct them manually.
If you read the documentation carefully at ?read.table (or ?read.csv) you will quickly see that there is an argument called check.names. You most likely want to set that to FALSE. Keep in mind, though, that those are not syntactically valid column names in R, so you you might actually prefer to change them to something that R will handle more smoothly anyway.

Resources