Unable to convert & assign character value to a numeric field - r

I have been struggling with the following issue:
I have the following variables:
class(HARdata)
[1] "data.frame"
dim(HARdata)
[1] 10299 88
class(activity_labels)
[1] "character"
length(activity_labels)
[1] 6
I have been trying to run the following loop:
for (i in 1:nrow(HARdata)) {
for (j in 1:length(activity_labels)){
if (as.numeric(HARdata[i, "traintype"]) == extract_numeric(activity_labels[j])) {
HARdata[i, "traintype"] <- activity_labels[j]
}
}
}
However, i get the following error:
Error in if (as.numeric(HARdata[i, "traintype"]) == extract_numeric(activity_labels[j])) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
NAs introduced by coercion
If I replace HARdata[i, "traintype"] <- activity_labels[j] with HARdata[i, "traintype"] <- 10 , the code runs fine. So I suppose the problem is in this line. The left side is numeric while the right side is supposed to be character. I tried running as.character(HARdata[i, "traintype"]) <- "test" but this doesn't work. Can anyone see what could be the issue?

test <- scan()
0.27513126 0.39694439
0.54228045 0.82751195
0.18600784 0.96602747
0.55259276 0.52368149
0.28976503 0.74500213
0.17534195 0.04931733
0.08077429 0.82169260
0.72602526 0.94921645
0.65077605 0.06989442
0.81399236 0.1379080
test <- as.data.frame(matrix(test, ncol=2))
names(test) <- c('cartype', 'traintype')
library(tidyr)
activity_labels <- c("$0.08077429", "$0.65077605")
test[,"traintype"][match(extract_numeric(activity_labels), test[,"traintype"])] <- activity_labels

Related

non-numeric argument to binary operator error while my data are numeric

I have following code to filter my data which is a large csv file. Running the following code, gives the error:
Error in gedi_table[, col_index] - gedi_table$digital_elevation_model_srtm :
non-numeric argument to binary operator" apears.
What is the solution? How can I fix it?
filter_gedi_table_2 = function(gedi_table, alg_num) {
## Function filtering GEDI table by removing shots with erroneous ground elevation
# gedi_table: data.frame
# alg_num: numeric
# Returns a data.frame
col_index = grep(paste0('elev_lowestmode_a', alg_num), colnames(gedi_table))
gedi_table_filter_2 = gedi_table[-which(abs(gedi_table[, col_index] - gedi_table$digital_elevation_model_srtm) > 100
& abs(gedi_table[, col_index] - gedi_table$digital_elevation_model) > 100), ]
return(gedi_table_filter_2)
}

Extraction of specific result in R outputs

I want to extract the values of "b1p" and "b2p" from the mardia's command and want to save it in bskew.
For this i have used the "psych" package R version is 4.0.3. I have tried several commands for extraction but failed.
bskew <- mardia$b1p
bskew <- mardia[b1p
bskew <- mardia[[b1p
for this i got the error "object of type 'closure' is not subsettable"
By using names() i got only names and by using class() i got "psych", "mardia".
By using summary() i got the message "Warning message:
In summary.psych(mardia(x)) :
I am sorry, I do not have a summary function for this object" and then i used mna$coefficients[[]] command
and i got the message "NULL".
I saved my mardia command in mna.
Minimum Working Example is:
n0 <- 5
p0 <- 2
m0 <- matrix(rep(0,p0),ncol=p0)
s0 <- diag(1,p0)
x <- rmvnorm(5,mean=m0, sigma=s0)
mardia$"b1p"
bskew <- mardia["b1p"]
bskew <- mardia[["b1p"]]
bkurt <- mardia[["b2p"]]
bskew <- mardia$b1p$
mna<-mardia(x)
class(mna)
names(mna)
summary(mardia(x))
summary(mna)
sk1 <- mna$coefficients[[3]]
mna$coefficients
the error is because you're trying to subset a function mardia which always throws the error you have, also you should subset the mna object instead of subsetting the actual function.
> mna$b1p
[1] 1.95888
> mna["b1p"]
$b1p
[1] 1.95888
> mna[["b1p"]]
[1] 1.95888
> mardia(x)$b1p
[1] 1.95888
> mardia$b1p
Error in mardia$b1p : object of type 'closure' is not subsettable
> mardia<-mardia(x)
> mardia$b1p
[1] 1.95888

quantmod <- Having trouble writing a formula to extract single day returns without headers

I am attempting to write a formula that will return a stocks single day return, but I believe im having trouble with the data type of the periodReturn subset field
periodReturn(ticker,period='daily',subset='20161010::20161010')
works but
dayReturn <- function(ticker,date) {
ticker <- c(MSFT)
date <- c(20161010)
dayreturn <- periodReturn(ticker,period='daily',paste("subset='",date,"::",date,"'"))
dayreturn
}
gives error
dayReturn(msft,20161010)
daily.returns
Warning messages:
1: In as_numeric(YYYY) : NAs introduced by coercion
2: In as_numeric(MM) : NAs introduced by coercion
3: In as_numeric(DD) : NAs introduced by coercion
>
Thanks in advance for any advice!
You have a couple of syntax errors going on here inside your dayReturn function.
Here is reproducible code extracted from inside your function that will work:
library(quantmod)
getSymbols("MSFT")
ticker <- c(MSFT)
date <- c("20161010")
dayreturn <- periodReturn(ticker,period='daily',subset = paste0(date,"::",date,"'"))
Your errors:
date wants to be a string, not a numeric number.
Your string for the dates you want to subset over is incorrect. you want to use subset = "YYYYMMDD::YYYYMMDD" or (subset = "YYYY-MM-DD::YYYY-MM-DD") in side periodReturn.
Your function would work more correctly like this:
dayReturn <- function(ticker, date1 , date2) {
dayreturn <- periodReturn(ticker,period='daily',subset = paste0(date1,"::",date2,"'"))
dayreturn
}
dayReturn(MSFT, "20161010", "20161012")
# daily.returns
# 2016-10-10 0.004152284
# 2016-10-11 -0.014645107
# 2016-10-12 -0.001398811

Using geterrmessage() in a loop - R

My objective here is to capture the error that R throws and store it in an object.
Here are some dummy codes:
for(i in 1:length(a)){try(
if (i==4)(print(a[i]/"b"))else(print(a[i]/b[i]))
)}
[1] -0.125
[1] -0.2857143
[1] -0.5
Error in a[i]/"b" : non-numeric argument to binary operator
[1] -1.25
[1] -2
[1] -3.5
[1] -8
[1] Inf
[1] 10
So I want to capture that on the 4th iteration the error was: Error in a[i]/"b" : non-numeric argument to binary operator into an object say:
error<-()
iferror(error[i]<-geterrmessage())
I am aware that iferror as a function is not available in R, but I am trying to give the idea, because geterrmessage captures only the last error it sees
So for the example i want say for error[1:3]<-'NA'and error[5:10]<-'NA' because no error but
error[4]<-"Error in a[i]/"b" : non-numeric argument to binary operator"
So that later I can check error object and understand where and what error happened
If you can help me write a code that would be excellent and highly appreciated
I hope the following function helps:
a <- c(0:6)
b <- c(-3:3)
create_log <- function(logfile_name, save_path) {
warning("Error messages not visible. Use closeAllConnections() in the end of the script")
if (file.exists(paste0(save_path, logfile_name))) {
file.remove(paste0(save_path, logfile_name))
}
fid <- file(paste0(save_path, logfile_name), open = "wt")
sink(fid, type = "message", split = F) # warnings are NOT displayed. split=T not possible.
sink(fid, append = T, type = "output", split = T) # print, cat
return(NULL)
}
create_log("test.csv", "C:/Test/")
for(i in 1:length(a)){try(
if (i==4)(print(a[i]/"b"))else(print(a[i]/b[i]))
)}
closeAllConnections()

Reading large fixed format text file in r

I am trying to input a large (> 70 MB) fixed format text file into r. For a smaller file (< 1MB), I can use the read.fwf() function as shown below.
condodattest1a <- read.fwf(impfile1,widths=testcsv3$Varlen,col.names=testcsv3$Varname)
When I try to run the line of code below,
condodattest1 <- read.fwf(impfile,widths=testcsv3$Varlen,col.names=testcsv3$Varname)
I get the following error message:
Error: cannot allocate vector of size 2 Kb
The only difference between the 2 lines is the size of the input file.
The formatting for the file I want to import is given in the dataframe called testcsv3. I show a small snippet of the dataframe below:
> head(testcsv3)
Varlen Varname Varclass Varsep Varforfmt
1 2 "V1" "character" 2 "A2.0"
2 15 "V2" "character" 17 "A15.0"
3 28 "V3" "character" 45 "A28.0"
4 3 "V4" "character" 48 "F3.0"
5 1 "V5" "character" 49 "A1.0"
6 3 "V6" "character" 52 "A3.0"
At least part of my problem is that I am reading in all the data as factors when I use read.fwf() and I end up exceeding the memory limit on my computer.
I tried to use read.table() as a way of formatting each variable but it seems I need a text delimiter with that function. There is a suggestion in section 3.3 in the link below that I could use sep to identify the column where every variable starts.
http://data.princeton.edu/R/readingData.html
However, when I use the command below:
condodattest1b <- read.table(impfile1,sep=testcsv3$Varsep,col.names=testcsv3$Varname, colClasses=testcsv3$Varclass)
I get the following error message:
Error in read.table(impfile1, sep = testcsv3$Varsep, col.names = testcsv3$Varname, : invalid 'sep' argument
Finally, I tried to use:
condodattest1c <- read.fortran(impfile1,lengths=testcsv3$Varlen, format=testcsv3$Varforfmt, col.names=testcsv3$Varname)
but I get the following message:
Error in processFormat(format) : missing lengths for some fields
In addition: Warning messages:
1: In processFormat(format) : NAs introduced by coercion
2: In processFormat(format) : NAs introduced by coercion
3: In processFormat(format) : NAs introduced by coercion
All I am trying to do at this point is format the data when they come into r as something other than factors. I am hoping this will limit the amount of memory I am using and allow me to actually input the file. I would appreciate any suggestions about how I can do this. I know the Fortran formats for all the variables and the column at which each variable begins.
Thank you,
Warren
Maybe this code works for you. You have to fill varlen with the field sizes and add the corresponding type strings (e.g. numeric, character, integer) to colclasses
my.readfwf <- function(filename,varlen,colclasses) {
sidx <- cumsum(c(1,varlen[1:(length(varlen)-1)]))
eidx <- sidx+varlen-1
filecontent <- scan(filename,character(0),sep="\n")
if (any(diff(nchar(filecontent))!=0))
stop("line lengths differ!")
nlines <- length(filecontent)
res <- list()
for (i in seq_along(varlen)) {
res[[i]] <- sapply(filecontent,substring,first=sidx[i],last=eidx[i])
mode(res[[i]]) <- colclasses[i]
}
attributes(res) <- list(names=paste("V",seq_along(res),sep=""),row.names=seq_along(res[[1]]),class="data.frame")
return(res)
}

Resources