I'm using an R script within Power Query to do some data transformations and return a scaled table.
My R code is like this:
# 'dataset'
It does seem like odd that this fails to return. A quick glance online gave this 3 minute youtube video, which uses the same method, which you are using. Further searching down a source, one may come across the Microsoft Documentation, which gives a possible reason for why there might be an issue.
When preparing and running an R script in Power BI Desktop, there are a few limitations:
Only data frames are imported, so make sure the data you want to import to Power BI is represented in a data frame
Columns that are typed as Complex and Vector are not imported, and are replaced with error values in the created table
These seem like the most obvious reasons. Betting that there is no complex columns in your dataset, I'd believe the prior is likely the reason. A quick recreation of your dataset shows that the scale functions changes your dataset into a matrix class object. This is kept by cbind, and as such output is of class matrix and not data.frame.
>dataset <- as.data.frame(abs(matrix(rnorm(1000),ncol=4)))
>class(dataset)
[1]"data.frame"
>library(dplyr)
>df_normal <- log(dataset + 1) %>%
> select(c(2:4)) %>%
> scale
>class(df_normal)
[1] "matrix"
>df_normal <- cbind(dataset[,1], df_normal)
>output <- df_normal
>class(output)
[1] "matrix"
A simple fix would then seem to be adding output <- as.data.frame(output), as this is in line with the documentation of powerBI. Maybe it would need a return like statement at the end. Adding a line at the end of the script simply stating output should fix this.
Edit
For clarification, I believe the following edited script (of yours) should return the data expected
# 'dataset' contém os dados de entrada neste script
library(dplyr)
df_normal <- log(dataset+1) %>%
select(c(2:4)) %>%
scale
df_normal <-cbind(dataset[,c(1)], df_normal)
output <- as.data.frame(df_normal)
#output ##This line might be needed without the first comment
Related
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 through assign() function to name many data frame.
Use this script:
> for (i in 1:15)
{
assign(paste0('TagIDNum',i),filter(Ordf,Ordf$TagID==i))
}
Got this 15 data frame
Next step I need to output scatterplot of these 15 data frames with pairs() function and for loop to output pdf in once.
Here is my script:
for (i in 1:15)
{
pdf(paste('TagPlotNum',j,'.pdf',sep=''))
x<-paste('TagIDNum',j,sep='')
print(pairs(~x[,11]+x[,38]+x[,39]+x[,40]+x[,41]+x[,43]))
dev.off()
}
But I got this error information
Error information: incorrect number of dimensions
And I found that the x had no data, just a value as follow:
I will do some analysis in next steps, so this problem disturb for 2 days.
Post this article to ask any expert to solve this issue.
In my opinion, maybe paste() function have something to think, but I don't know how to solve this topic.
Here is my R information:
Thanks.
As per your output, x is the string "TagIDNum11", not the object with that name. You can get that however using get(), i.e.
x<-get(paste('TagIDNum',j,sep=''))
FYI, spaces are free, your code will be much more readable if you use them, i.e.
x <- get(paste('TagIDNum', j, sep=''))
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 have long dataset where I want to experiment with different settings for the seqefsub() function, and depending on the setting, one run can take relatively long. Therefore I want the computer to calculate all the different variations and later evaluate the results, evtl. use them for further processing.
My problem is when I save the results in a file and load them, the structure of the data appears to be broken. As a result I cannot use the TraMineR functions on this data after I load it, hence I need to reproduce all the calculations every single time after closing R.
Saving to the workspace with RStudio (.RData) gives the same error. Saving to binary format gives the same error.
This is how the sequence list looks like in RStudio, before saving:
And after loading:
This is the code I used for this example:
library(TraMineR)
data(actcal.tse)
seqe <- seqecreate(actcal.tse[1:100, ])
fsub <- seqefsub(seqe, minSupport = 0.1)
save(fsub, file="fsub.rda")
rm(fsub)
load("fsub.rda")
Details of my system:
x86_64-pc-linux-gnu (Ubuntu 14.04 LTE)
R version 3.2.0 (2015-04-16)
RStudio Version 0.98.1103
TraMineR stable version 1.8-9 (Built: 2015-04-22)
If you check the value returned from the seqefsub() it is a subseqelist object. This kind object contains other objects listed in the docs as:
seqe: The list of sequences in which the subsequences were searched (a seqelist event sequence object).
subseq: A list of subsequences (a seqelist event sequence object).
data: A data frame containing details (support, frequency, ...) about the subsequences
And others. What I did to save the results was to convert the data that I needed in to lists and build a data frame with them before save it.
library(TraMineR)
data(actcal.tse)
seqe <- seqecreate(actcal.tse[1:100, ])
fsub <- seqefsub(seqe, minSupport = 0.1)
#Get the data I need only
#(Explore the other objects to get what you need)====
#Gets the column support from data (which is a data frame)
support <- fsub$data$Support
#subseq is class that cannot be converted to a data frame
#it stores de subsquences found and I will convert them to strings
sequences <- as.character(fsub$subseq)
#Builds the data frame
result <- data.frame(sequences, support)
#Save it at root
save(result, file="~/result.rda")
rm(result)
load('~/result.rda')
I hope it still helps you.
I'm very new to R and am working on a text mining project. I have all the analysis working however when I convert the Term-Document Matrix back to a data frame it fills the console with the content..
The code I'm using for this is:
TDM.frame <- data.frame(inspect(Words.TDM))
The frame has 9k objects in it so I won't paste that here too but you can imagine what the console looks like when it dumps the whole content out ^^
I've tried using invisible() but that doesn't change anything. I hope someone can tell me what I'm doing wrong, or offer a solution!
Thanks!
This is what inspect does (at least in the case where it is given a TDM):
> tm:::inspect.TermDocumentMatrix
function (x)
{
print(x)
cat("\n")
print(as.matrix(x))
}
<environment: namespace:tm>
So you want the object that is returned which is just as.matrix(tdm) and you do not want the printing side-effect. So you should just do this instead:
TDM.frame <- data.frame(as.matrix(Words.TDM))
Is the inspect() within data.frame() really necessary? Can you perhaps just convert the TDM to a matrix, since it seems this is what you are trying to achieve? If necessary, you can then convert the matrix to a data frame.
as.matrix(Words.TDM)
Try
TDM.frame <- data.frame(inspect(Words.TDM))
head(TDM.frame)
Or, you can use dplyr
library(dplyr)
TDM.frame <- tbl_df(TDM.frame)