If i try to load a dataset using data it gets loaded as promise. And if I try to assign that dataset to a variable only the name of dataset gets assigned as a character vector. See screenshot for example. The commands used are:
data("AirPassengers") #this loads AirPassengers as promise
df = data("AirPassengers")
I can also use the same dataframe using datasets::AirPassengers which loads/returns in dataframe format.
My question are:
What is promise?
Why in assignment is only the name of dataframe getting assigned as character when using data().
Is there any advanatge of loading data with data() compared to the usual datasets::dataframeName.
Related
I am trying to import dynamodb in R using paws.database library. I am successful in retrieving the required attribute into R(using scan operation). However the imported data is in the form of a nested list i.e. in [[]] form. My intention is to format the imported dynamodb attribute into a dataframe and later be able to plot it using ggpplot. I have tried using options such as
df <- ldply (list_a, data.frame), ldply (list_a, data.frame),
data.frame(matrix(unlist(list_a), nrow=length(list_a),
byrow=TRUE),stringsAsFactors=FALSE),
as.data.frame(do.call(cbind,list_a))
so far and was unable to convert the data in a proper dataframe format. The final error I get in ggplot is "
Error: data must be a data frame, or other object coercible by fortify(), not a list "
Could anyone please help ?
See this similar issue.
I'm also using paws. Here's what I did to work with a small DynamoDB table:
dyna <- paws::dynamodb()
Table<-dyna$scan("table_name")
newtable<-rbindlist(Table$Items,fill = TRUE)
Then I create a new dataframe by using unlist() on each column of newtable.
For some reason, when I run a line assigning columnn names to my dataframe (df) from another data frame (nm), I can no longer view my columns using the "$" operating; instead when I put "df$" I get the following error: Cannot read property 'substr' of Null.
Loading either dataset does not produce this problem, only when I assign column names to df using the following line:
colnames(df) = nm$Var_Code
This problem has not been happening before when running this code and is rather new. I'm not sure how to approach the problem and any assistance would be appreciated.
I am also new to R-studio, the way I get over it is to write the row names on the first row in textfile, and import data from textfile with specifying row names of data frame to be the first row of textfile.
I am trying to take R code, stored in cells of the content column of a dataframe, and analyze the functions used by applying the Tidycode package. However, I first need to convert the data to a Matahari tibble before applying an unnest_calls() function.
Here is the data:
data <- read.csv("https://github.com/making-data-science-count/TidyTuesday-Analysis/raw/master/db-tmp/cleaned%20database.csv")
I have tried doing this in a number of different ways, including extracting each row (in the content column ) as an Rfile and then reading it back in with Tidycode calls, for example:
tmp<-data$content2[1])
writeLines(tmp, "tmp.R") #I've also used save() and write()
rfile<-tidycode::read_rfiles("tmp.R")
But, I keep getting errors such as: "Error in parse(text = x) : <text>:1:14: unexpected symbol
1: library(here)library"
Ultimately, what I would like to do is analyze the different types of code per file, and keep that linked with the other data in the data dataframe, such as date and username.
Any help would be greatly appreciated!
I want to load dataset from package ElemStatLearn in R studio.
But when I load the dataset, my Global Environment panel shows
library("ElemStatLearn")
data("nci")
However, when I execute
View("nci")
I can see the whole data but cannot export it to a dataframe.
How can I convert or export this dataset into a dataframe?
You can do
df <- data.frame(nci)
Another way to go around would be
df <- get(data("nci"))
If you had done anything with the name nci that required it's modification or evaluation, the R engine would have at that point pulled in the values and you would no longer have had a promise. Instead, you asked to look not at an R name but at an R literal character value. The value of "nci" is just "nci". The value of nci on the other hand has 6,830 entries when I try to look at it.
The data function can accept a character value for purposes of retrieving an externally stored object, but the View function expects a real (unquoted) R name. Or you could have used: View(as.name("nci") )
I can't figure out why this version of plyr's rename function isn't working.
I have a dataframe where I have a single column that ends up being named seq(var_slcut_trucknumber_min, var_slcut_trucknumber_max) because I made it like this:
df_metbal_slcut <- as.data.frame(seq(var_slcut_trucknumber_min,var_slcut_trucknumber_max))
The terms var_slcut_trucknumber_min and var_slcut_trucknumber_max are defined as the min and max of another column.
However, when trying to rename it by the following code,
var_temp <- names(df_metbal_slcut)
df_metbal_slcut <- rename(df_metbal_slcut, c(var_temp="trucknumber"))
I get an error as follows:
The following `from` values were not present in `x`: var_temp
I don't understand why. I know that I can easily do this as colnames(df_metbal_slcut)[1] <- "trucknumber", but I'm an R n00b, and I was looking at a data manipulation tutorial that said that learning plyr was the way to go, so here I am stuck on this.
Try this instead:
df_metbal_slcut <- rename(df_metbal_slcut, setNames("trucknumber",var_temp))
The reason it wasn't working was that c(var_temp = "trucknumber") creates a named vector with the name var_temp, which is not what you were intending. When creating named objects using the tag = value syntax, R won't evaluate variables. It assumes that you literally want the name to be var_temp.
More broadly, it might make sense to name the column more sensibly when initially creating the data frame again using setNames.