Replacing a / character in a variable in R - r

I have a column of a few thousand prison facility names that have been scraped from the internet, some of which contain a "/" character. I'm running a ggplot loop that plots COVID infections in each facility, but the ggsave() function breaks whenever it hits a facility with the / because it thinks I am changing the file directory.
Is there a way to change the "/" character in my column to a "-"? I tried using gsub() but to no avail:
(1)
fix$Name <- gsub("\/"","-",gsub$Name)
I get the following error (2)
Error in gsub$Name : object of type 'closure' is not subsettable
I initially tried to just run (3)
fix$Name <- gsub("/","-",gsub$Name)
But I am fairly sure that I need an escape character, hence code line (1).
fix$Name is a character string.
If anyone has an insight that would be greatly appreciated! Thanks!

Related

Problem with analysis with special characters in getsymbols (PerformanceAnalytics)

I am trying to create a chart.correlation with the package PerformanceAnalytics. This works great like here:
if(!require("PerformanceAnalytics")) {install.packages("PerformanceAnalytics"); require("PerformanceAnalytics")}
library(PerformanceAnalytics)
#
getSymbols("AMZN",from="2016-01-01",to="2020-05-01")
getSymbols("TSLA",from="2016-01-01",to="2020-05-01")
data1<-cbind(diff(log(Cl(AMZN))),diff(log(Cl(TSLA))))
chart.Correlation(data1)
But as soon as there are special characters I get the following error:
Error: unexpected '^' in "data<-cbind(diff(log(Cl(UTDI.DE))),diff(log(Cl(^"
for the following code:
getSymbols("UTDI.DE",from="2016-01-01",to="2020-05-01")
getSymbols("^MDAXI",from="2016-01-01",to="2020-05-01")
data2<-cbind(diff(log(Cl(UTDI.DE))),diff(log(Cl(^MDAXI))))
chart.Correlation(data2)
Does anyone know how to use the special character here?

rafalib - as.fumeric error " 'x' must be a character"

Generating a plot with rafalib open.
Have a dataset with a column labeled "Tissue". The entire table is in object "b". "hc" contains a hclust of the distribution of the numeric values of "b".
When I run:
myplclust(hc, xlab="distance",main="Hierarchical Clustering Dendrogram",labels=b$Tissue,lab.col=as.fumeric(b$Tissue),cex=0.5)
RStudio responds with:
Error in as.fumeric(b$Tissue) : 'x' must be a character
What's going on here? I've reset R multiple times. I have rafalib installed and active.
SOLVED:
The names I was entering under this function were being recognized as data rather than values for some reason. By converting my list to a vector with as.vector inside of the as.fumeric function, the problem was solved.
The correct code now looks like;
myplclust(hc, xlab="distance",main="Hierarchical Clustering Dendrogram",labels=b$Tissue,lab.col=as.fumeric(as.vector(b$Tissue)),cex=0.5)

Error in eval(expr, envir, enclos) : object 'score' not found

We have always been an SPSS shop but we're trying to learn R. Been doing some basics. I am trying to do a simple t-test, just to learn that code.
Here's my code, and what's happening:
Code screenshot
I don't get why it says "score" isn't found. It's in the table, and the read.csv code defaults to assuming the first row contains headers. I can't see why it's not "finding" the column for score. I'm sure it's something simple, but would appreciate any help.
You didn't store your imported csv file in a variable. It printed to the console because it had nowhere else to go - it just gets printed and then thrown away. You need to assign it for it to be saved in memory:
my_data_frame <- read.csv("ttest.csv")
Now your data exists in the variable my_data_frame and you can use it by supplying it as the data argument:
t.test(score ~ class, mu=0, alt="two.sided", conf=0.95, var.eg=F, paired=F, data=my_data_frame)
Also, in general, I would recommend using read_csv from the package readr over the default read.csv - it's faster.
Finally, when you ask questions, please provide your code as text, not a picture. You should also provide your data or a toy dataset - you can use the function dput to print code that will create your data, or just provide the csv file or some code that creates toy data.

Large data.tree causes plot() to error

I'm trying to build an org chart from a data.frame in r using the data.tree package.
As far as i can tell i have constructed the tree correctly, but when I try to plot() the data.tree object (which print()s fine) I get an error:
abort(0) at jsStackTrace#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:22110
stackTrace#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:22258
abort#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:28:10656
nullFunc_iii#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:662065
a8#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:21:31634
iC#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:83383
aD#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:102098
uF#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:173805
pG#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:204484
xc#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:11:740
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:28:403
ccallFunc#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:15982
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:47:42
renderValue#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/grViz-binding-0.8.4/grViz.js:38:27
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:625:30
forEach#[native code]
forEach#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:55:21
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:551:14
forEach#[native code]
forEach#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:55:21
staticRender#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:549:12
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:638:38
Any ideas?
I have just started using the data.frame package a couple of days ago, but came across the same problem with some of my trees - print(tree) worked but plot(tree) gave a similar error message to yours. So far, I could always resolve the issue by removing special characters like single and double quotation marks from the input data. Thus, the plot function appears to be sensitive to certain symbols or special characters ... maybe a starting point for your search for a solution?

dplyr rename command with spaces

I have tried multiple variations of the rename function in dplyr.
I have a data frame called from a database called alldata, and a column within the data frame named WindDirection:N. I am trying to rename it as Wind Direction. I understand creating variable names containing spaces is not a good practice, but I want it to be named as such to improve readability for a selectInput list in shiny, and even if I settle for renaming it WindDirection I am getting all of the same error messages.
I have tried:
rename(alldata, Wind Direction = WindDirection:N)
which gives the error message:
Error: unexpected symbol in "rename(alldata, Wind Direction"
rename(alldata, `Wind Direction` = `WindDirection:N`)
which does not give an error message, but also does not rename the variable
rename(alldata, "Wind Direction" = "WindDirection:N")
which gives the error message:
Error: Arguments to rename must be unquoted variable names. Arguments Wind Direction are not.
I then tried the same 3 combinations of the reverse order (because I know that is how plyr works even though I do not call it to be used using the library command earlier in my code) putting the old variable first and the new variable 2nd with similar error messages.
I then tried to specify the package as I have 1 example below and tried all 6 combinations again.
dplyr::rename(alldata, `Wind Direction` = `WindDirection:N`)
to similar error messages as the first time.
I have used the following thread as an attempt to do this myself.
Replacement for "rename" in dplyr
as agenis pointed out, my mistake was not redefining the dataframe after renaming the variable.
So where I had
dplyr::rename(alldata,Wind Direction=WindDirection:N)
I should have
alldata <- dplyr::rename(alldata,Wind Direction=WindDirection:N)

Resources