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") )
Related
I am using a summary() function from an R package. It prints out multiple results by using cat() and print() functions interweavingly (I checked its source code by getAnywhere()). It uses cat() to output descriptive messages, and uses print() to print out the dataframe and the matrix that I wanted to save. I am using R notebook, so I can see the cat() output and matrix output in the R Console tab, and the dataframe output by itself in another result tab. I wasn't able to save or retrieve the dataframe.
I tried sink() and capture.output() functions, but it only saved the descriptive messages and matrix together in a text file, and no dataframe saving. Does anyone know how to retrieve the dataframe object by itself? And I may also need to retrieve the matrix object by itself too.
The R package is called "lmmlasso". I guess it's not very well maintained. Here I am just giving an example code:
summary.foo= function(){
cat("random effects\n")
print(matrix(rnorm(9),3))
cat("fixed effects \n")
print(data.frame(X = c("A","B","C"), Estimate = rnorm(3)))
}
summary.foo()
I was not able to solve it directly, so I turned to the alternative way: I copied the original source code, return the dataframe and matrix objects at the end, and changed its function name. I got the dataframe by using the new summary function.
You should provide a reproducible code.
Anyway, try to assign it to a variable and then use the variable attributes, either with $ or #, to access the output you desire
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.
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 using the Alteryx R Tool to sign an amazon http request. To do so, I need the hmac function that is included in the digest package.
I'm using a text input tool that includes the key and a datestamp.
Key= "foo"
datastamp= "20120215"
Here's the issue. When I run the following script:
the.data <- read.Alteryx("1", mode="data.frame")
write.Alteryx(base64encode(hmac(the.data$key,the.data$datestamp,algo="sha256",raw = TRUE)),1)
I get an incorrect result when compared to when I run the following:
write.Alteryx(base64encode(hmac("foo","20120215",algo="sha256",raw = TRUE)),1)
The difference being when I hardcode the values for the key and object I get the correct result. But if use the variables from the R data frame I get incorrect output.
Does the data frame alter the data in someway. Has anyone come across this when working with the R Tool in Alteryx.
Thanks for your input.
The issue appears to be that when creating the data frame, your character variables are converted to factors. The way to fix this with the data.frame constructor function is
the.data <- data.frame(Key="foo", datestamp="20120215", stringsAsFactors=FALSE)
I haven't used read.Alteryx but I assume it has a similar way of achieving this.
Alternatively, if your data frame has already been created, you can convert the factors back into character:
write.Alteryx(base64encode(hmac(
as.character(the.data$Key),
as.character(the.data$datestamp),
algo="sha256",raw = TRUE)),1)
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.