How to fix 'non-numeric argument to mathematical function' - r

I am totally new in R and doing an assignment for my studies. While coding I found an error stating "non-numeric argument to mathematical function". I Know the header is causing it, as it is the only non-numeric character in the database. But I don't know how to fix it.
I tried to calculate excluding the header, but couldn't do it.
DF = read.csv("Nominal Broad Dollar Index Modified.csv", header = TRUE)
head(DF)
This is the head of my data frame, & I'm not sure where the x1 column came and whether it is causing the error.
Index = DF$Index
Index0 = Index[DF$Y < 2018]
R0 = diff(log(Index0))
Error in log(Index0) : non-numeric argument to mathematical function
there are no results, I'm just getting errors.

Related

Error in if (xhat[i] == 0) xhat[i] <- 0.01 : missing value where TRUE/FALSE needed

This is my code:
set.vertex.attribute(formal_network_data_sociomatrix, names(Attributes_Members), Attributes_Members)
list.vertex.attributes(formal_network_data_sociomatrix)
get.vertex.attribute(formal_network_data_sociomatrix,"vertex.names")
get.vertex.attribute(formal_network_data_sociomatrix,"team")
formal_network_data_sociomatrix
Attributes_Members$degree <- degree(formal_network_data_sociomatrix,)
Attributes_Members
Attributes_Members$closeness <-closeness(formal_network_data_sociomatrix,)
Attributes_Members$betweenness <- betweenness(formal_network_data_sociomatrix,)
Attributes_Members
write.csv(Attributes_Members, file="AttributseavgCloseness.csv", row.names = TRUE)
cutpoints(formal_network_data_sociomatrix,)
degree_of_separation<-geodist(formal_network_data_sociomatrix)
mean(degree_of_separation$gdist)
max(degree_of_separation$gdist)
dep <- get.vertex.attribute(formal_network_data_sociomatrix, "team")
dep_nodes<-Attributes_Members$team
dep_nodes[dep_nodes == "2"] = 2
sides <- 3
color_pallet <- c("red")
set.vertex.attribute(formal_network_data_sociomatrix, names(Attributes_Members), Attributes_Members)
ego_network_branddevelopment <- get.inducedSubgraph(formal_network_data_sociomatrix, c(4))
gplot(ego_network_branddevelopment, displaylabels=T)
here ends my code.
in the last line "'gplot(ego_network_branddevelopment, displaylabels=T)'" i get the error "Error in if (xhat[i] == 0) xhat[i] <- 0.01 :
missing value where TRUE/FALSE needed"
and if i look it up anywhere it's says to do something with an if function but there is no if function.
i want to construct a network where all my ties are red and that just my ties are in the network.
Can anyone help me with fixing my error
The error message "Error in if (xhat[i] == 0) xhat[i] <- 0.01 : missing value where TRUE/FALSE needed" suggests that there may be missing or undefined values in the data that you are trying to plot.
One possible cause of this error could be that your data contains missing or NA values, which are not allowed in if conditions. You can check your data by running the following command:
sum(is.na(Attributes_Members))
This will return the number of missing values in the dataframe 'Attributes_Members'.
Another possible cause of this error is that the 'displaylabels' argument in the gplot function is not correctly set. The 'displaylabels' argument is used to show or hide the labels of the vertices in the network plot. The value of this argument should be either 'TRUE' or 'FALSE' and not a missing value or NA.
In order to fix this error, you can try to remove any missing values from the dataframe 'Attributes_Members' before plotting the network. You can do this by running the following command:
Attributes_Members <- Attributes_Members[complete.cases(Attributes_Members),]
This will remove all the rows from the dataframe that contain any missing values.
Alternatively, you can try to set the displaylabels argument to 'FALSE' in the gplot function like this:
gplot(ego_network_branddevelopment, displaylabels=FALSE)
Also, you can check the data of the ego_network_branddevelopment object and remove any missing value if they exist.
If that doesn't work, please provide more information about the data you're working with, and the specific error message you're seeing, as well as the library you're using.

prcomp() function is giving me error "infinite or missing values in 'x'"

Eset is an expression matrix and I'm trying to make a PCA and I keep on getting this error and I was thinking maybe it was because "exprs" is not numeric but I'm checking and it is double. how can I solve this?
Eset<-ExpressionSet(as.matrix(exp))
pData(Eset)<-meta
featureData(Eset) <- as(feat,"AnnotatedDataFrame")
exprs<- Biobase::exprs(Eset)
exprs<-t(exprs)
exprs<- as.numeric(exprs)
type(exprs)
PCA <- prcomp(exprs, scale = FALSE)
I was not expecting this error because I made sure that "exprs" was numeric but it's still not working

Numeric data gives error non-numeric argument to binary operator in R

New to R.
I have a dataframe called SortedDF with 125143 observations and 5 variables
Head(SortedDF)
number Retention time (min) Charge m/z Group
28637 98481 16.87978 2 350.1859 Progenesis_peptide
82465 DVQLPK 14.35000 2 350.2022 PEAKS_peptide
81468 DVQLPK 14.32000 2 350.2027 PEAKS_peptide
76662 DVQLPK 14.33000 2 350.2028 PEAKS_peptide
77423 DVQLPK 14.36000 2 350.2029 PEAKS_peptide
73768 DVQLPK 14.27000 2 350.2039 PEAKS_peptide
I want to match peptides based on their m/z similarity using lead command.
MatchedDF <- SortedDF %>% mutate(matches_with_next_row = (abs(("m/z") - lead("m/z")) < 0.01))
While coding another database, this code worked perfectly. However right now, I get the error message saying
Error in mutate(): ! Problem while computing matches_with_next_row = ... & Group != lead(Group). Caused by error in ("m/z") - lead("m/z"): ! non-numeric argument to binary operator Run
rlang::last_error() to see where the error occurred.
When checking
class(SortedDF$"m/z")
(1) "numeric"
I checked following threads without succes:
x non-numeric argument to binary operator while using mutate
--> tried '' around m/z without succes
Other threads mainly talk about other problems than mine. If anyone has an idea on what I am doing wrong, please tell me.
"m/z" is a non-numeric character, whereas m/z with back ticks refers to a data variable of the particular column:
MatchedDF <- SortedDF %>% mutate(
matches_with_next_row = (abs((`m/z`) - lead(`m/z`)) < 0.01)
)
It is recommended to not use special characters for column names to make these expressions much easier to write.

Compute the mean in R subject to conditions

I tried following the advice on this post (Conditional mean statement), however it did not seem to work for me. I get the error Error in x[j] : only 0's may be mixed with negative subscripts.
So I have a database called data with a few different columns and many rows. There is one indicator column, z, which takes value 0 or 1. I want to compute the mean of the column base if z depending on whether z=0 or z=1. So I have used the following line of code:
mean(data[data$z==1, data$base], na.rm = TRUE)
But as mentioned, I get the error Error in x[j] : only 0's may be mixed with negative subscripts. I'm unsure why I am getting this error, or what I could/should do instead. I do not actually understand the error.
Thanks.
Using the comment by GKi worked and solved my problem. In the end I used
mean(data$base[data$z==1], na.rm = TRUE)

"Error in 1:ncol(x) : argument of length 0" when using Amelia in R

I am working with panel data. I have well over 6,000 country-year observations, and have specified my Amelia imputation as follows:
(CountDependentVariable, m=5, ts="year", cs="cowcode",
sqrts=c("OtherCountVariable2", "OtherCount3", "OtherCount4"),
ords=c("OrdinalVar1", "Ordinal Variable 2"),
lgstc=c("ProportionVariale"),
noms=c("NominalVar1"),p2s = 0, idvars = c("country"))
When I run those lines of code, I continue to receive the following error:
Error in 1:ncol(x) : argument of length 0
I've seen people get a similar error, but in different contexts. Importantly, there are several continuous independent variables I left out of the Amelia code, because I am under the impression that they get imputed WITHOUT having to do so. Does anyone know:
1) What this error means?
2) How to correct this error?
Update #1: Provided more context, in terms of the types of variables in my count panel data, in the above sample code.
Update #2: I did some research, and ran into an R file containing a function that diagnoses possible errors for Amelia code. After running the code, I got the following error message first (and many more thereafter):
AMn<-nrow(x)
Error in nrow(x) : object 'x' not found
AMp<-ncol(x)
Error in ncol(x) : object 'x' not found
subbedout<-c(idvars,cs,ts)
Error: object 'idvars' not found
Error Code: 4
if (any(colSums(!is.na(x)) <= 1)) {
all.miss <- colnames(x)[colSums(!is.na(x)) <= 1]
if (is.null(all.miss)) {
all.miss <- which(colSums(!is.na(x)) <= 1)
}
all.miss <- paste(all.miss, collapse = ", ")
error.code<-4
error.mess<-paste("The data has a column that is completely missing or only has one,observation. Remove these columns:", all.miss)
return(list(code=error.code,mess=error.mess))
}
Error in is.data.frame(x) : object 'x' not found
Error codes: 5-6
Errors in one of the list variables
idout<-listcheck(idvars,"One of the 'idvars'")
Error in identical(vars, NULL) : object 'idvars' not found
Currently, there are no missing values for the country variable I place in the idvars argument. However, the very first "chunk" of errors wants me to believe that this is so.
Am I not properly specifying the Amelia code I have above?
I had forgotten to specify the dataframe in the original Amelia code (slaps hand on forehead). So now, after resolving the whacky issue above, I am getting the following error from Amelia:
Amelia Error Code: 44
One of the variable names in the options list does not match a variable name in the data.
I've checked the variable names, and they match, verbatim, to what I named them in the dataframe.

Resources