Error in `[.data.frame`(t, 1) : undefined columns selected - r

I got this R code to make visualization using ggplot. However, I am getting the problem with syntax. Can anyone see where it went wrong? Thanks!
t <- ggplot_build(p)$data[[3]]
s[i,1] <- as.character(a[i])
s[i,2] <- t[1,2] - t[(length(t[,1])),2]
The error is related to t in this three lines of code.

Related

I cannot apply functions. Error: object not found

I'm new to R and I'm following videos to import my data. Now, I'm having a hard time in applying some functions, like mean, summary, view (column) etc. It always shows the error: "object" is not found. I've tried many things but I'm stucked here for 2 days!
Can you help me? Thanks a lot!
#Video https://www.youtube.com/watch?v=cnD1op2Oo3M&list=PLtL57Fdbwb_Chn-dNR0qBjH3esKS2MXY3&index=4&ab_channel=RProgramming101
#Create the WD and view the table
library(readxl)
TMA_data <- read_excel("C:/Users/p1160413/Desktop/TMA R studio/TMA
data/dataforR.xlsx",
sheet = "Sheet2")
View(TMA_data)
#Test if it find the table and applies function
TMA_data
MeanAge <- mean(Age)
Result: Error in mean(Age) : object 'Age' not found

How to solve this error message: attempt to select less than one element in OneIndex

I am using mFD package to run a code. I am using two datasets to run this code. One is :
and the another data is:
My code look like:
alpha_fd_indices <- mFD::alpha.fd.multidim(
sp_faxes_coord = sp_faxes_coord [ , c("PC1", "PC2", "PC3", "PC4", "PC5")],
asb_sp_w = as.matrix(species),
ind_vect = c("fdis", "fmpd", "fnnd", "feve", "fric", "fdiv", "fori", "fspe", "fide"),
scaling = TRUE)
And the error message is:
Error in sp_coord_all_asb[[k]] <- sp_faxes_coord_k :
attempt to select less than one element in OneIndex
Can anyone help me to solve this problem?
After struggling with this issue myself I found that the mFD package requires row names for your 'asb_sp_w' matrix here.
From the code for the 'alpha.fd.multidim' function I found that this function runs a loop for each of the community rows of the asb_sp_w matrix but it does this by getting the rownames() from this matrix in the line:
k <- rownames(asb_sp_w)[n].
My workaround was to give each of the rows some fake row names (or your community sample names) and this should allow the function to run without error.
I'm not sure if there is a practical usage for mFD to keep this in the function or not so maybe someone else can comment on this.

Why am I receiving "plow.new has not been called yet" when trying to plot PCAs?

This is the end goal however when trying to plot the code below
plot(PCAloadings[,1:2], arrows(0,0,PCAloadings[,1],PCAloadings[,2]), text(PCAloadings[,1:2], labels=rownames(PCAloadings))
I recieve the following error:
in text.default(PCAloadings[, 1:2], labels = rownames(PCAloadings)) : plot.new has not been called yet
I am unsure how to resolve this issue or what it pertains to, any help is welcome.
EDIT: when trying to run line by line only the first line runs. When trying to run arrows or text lines I run into the same error
plot(PCAloadings[,1], PCAloadings[,2])
This is the output from the first line alone
Seems to be a syntax problem. Try
plot(PCAloadings[,1:2])
arrows(0,0,PCAloadings[,1],PCAloadings[,2])
text(PCAloadings[,1:2], labels=rownames(PCAloadings))

What causes this? rdplot Error in R: Error in seq.default(x_min, c, jump_l) : invalid '(to - from)/by'

I am trying to use some very simple data in rdplot but I keep getting the error: Error in seq.default(x_min, c, jump_l) : invalid '(to - from)/by' I found this response from a diffrent post on Stack Overflow but can seem to apply the fix to the rdplot function. does anyone know how can this be fixed?
The actual code I am using is:
library(rdrobust)
rdplot(y = dt$treated, x = dt$score)
Reproducibility:
Here is a sample of my data, as I've said it is fairly common data. So far I have found a couple of things:
There are multiple subsets of the observations that seem to cause problems
Changing the nbins or binselect arguments from their default will fix the problem
The first time you get an error is on the set 1:1463, however using only observations 2:1464 is OK.
The recent rdrobust version 1.0.1 should take care of this error.

How do you remove NA's in geom_bar when plotting data subsets?

this is related to the question here, but the proposed solutions don't work in my case.
I've already posted a question regarding my large data.frame here, but if you just want to download it (>2000 rows) please do so via this link.
So the following code is used to create a simple geom_barbarplot in ggplot2
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SoExample$Structural_motif=="NDBL/beta-sheets",],
aes(x=p53_IHC))
gghist+geom_bar()
This produces this plot:
As you can see, the NA's are plotted as well. I've tried various options to remove the NA's including the following:
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=p53_IHC), drop=TRUE)
gghist+geom_bar()
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=p53_IHC), na.rm=TRUE)
gghist+geom_bar()
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=p53_IHC, na.rm=TRUE))
gghist+geom_bar()
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=factor(p53_IHC), na.rm=TRUE))
gghist+geom_bar()
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=factor(p53_IHC))
gghist+geom_bar(na.rm=TRUE)
And then I tried this:
gghist_2<-ggplot(na.omit(ARCTP53_SOExample[ARCTP53_EsoMutClean$Structural_motif=="NDBL/beta-sheets",]), aes(x=p53_IHC))
gghist_2+geom_bar()
Which gives me this error:
Error in as.environment(where) : 'where' is missing
Further, I tried this, which gives me the following errors
datasub<-ARCTP53_SOExample[!is.na(ARCTP53_SOExample)]
gghist_3<-ggplot(datasub[datasub$Structural_motif=="NDBL/beta-sheets",])
Error in datasub$Structural_motif :
$ operator is invalid for atomic vectors
And this code doesn't work either:
gghist_3<-ggplot(datasub, aes(x=p53_IHC))
Error: ggplot2 doesn't know how to deal with data of class character
So, does anyone know an easy solution to this? The data.frame is big and inherently has a lot of missing data depending on which column I'm looking at, but not all missing data is missing across all rows, so deleting any row that has a single "NA" in it, would be defeating the point.
Help is much appreciated.
Kind regards,
Oliver
EDIT Following Daniel's comments below, this what I get following the code below. As you can see the problem is still not solved. Sorry.
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets" & is.na(ARCTP53_SOExample$p53_IHC) == F,], aes(x=p53_IHC))
gghist+geom_bar()
UPDATE: Re-Ran the code and for some reason it works now. Got the plot below:
I couldn't downlaod your data, but this should work:
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SoExample$Structural_motif=="NDBL/beta-sheets" & is.na(ARCTP53_SOExample$p53_IHC) == F,], aes(x=p53_IHC))
gghist+geom_bar()

Resources