target of assignment expands to non-language object
I'm not sure which language you are coming from (SAS maybe?) but R is a proper functional programming language and doesn't use things like macros to automate tasks. Here's a more R-like way to approach the problem
no <- 2
results <- lapply(1:no, function(k) {
# use aggregate function to make correlation calculation.
this_dat_mean <- aggregate(data[,c(paste0("y", c("f","p","c"), "_",k))], list(data$id), mean)
this_cor <- cor(this_dat_mean, use = "complete.obs")
#write.table(this_cor, "file_path", row.names=T, col.names=T, quote=F)
# calculate the lm
this_lm_cal <- as.data.frame(this_dat_mean)
this_lm <- lm(reformulate(paste0("yc_",k), paste0("yf_",k)), data = this_lm_cal)
#write.table(this_lm, "file_path2", row.names=T, col.names=T, quote=F)
list(lm=this_lm, cor=this_cor)
})
Notice that we use a function to iterate over the inputs of interest. This function has a bunch of local variables. We can return a list of values that we want to preserve from the function. We can get at them by looking at
results[[1]]$lm
results[[2]]$cor
for example. It's better to create a (possibly named) list of values in R than to create a bunch of similarly named variables.
The lm
model isn't a data.frame so you can't use write.table
with that. Not sure what the goal was there.
Error: Target of assignment expands to non-language object, when using eval(parse())
The error message is obscure but what this essentially tells us is that you can’t perform assignments to arbitrary expressions. eval(parse(…))
isn’t a name, you can’t assign to it.1
The fundamental problem you want to solve doesn’t require any of that, luckily (because otherwise you’d be in a right pickle). Instead, you need to perform [[
subsetting rather than $
subsetting:
Repomid[[paste0('MidPrs_', namesV[1L])]] <- Repomid[[paste0('MidPrs_', namesV[2L])]]
Simply put, the syntactic form foo[['bar']]
is (more or less) equivalent to foo$bar
, where bar
is a quoted string in the first expression, but an unquoted name in the second one.
1 It’s (quite a lot) more complicated than that, because you can assign to expressions in R which aren’t pure names. But ultimately they all need to resolve to a name somehow. The above doesn’t. But even if it did it probably wouldn’t work as you’d expect because you’re attempting to assign to the result of that eval
call. And the result of that eval
call is a value, not a name. R fundamentally doesn’t allow assigning to values.
target of assignment expands to non-language object in R
I'd start by adding spaces: rho[1] <-1
presumably was meant to be rho[1] < -1
but what you got here is rho[1] <- 1
.
Related Topics
Assign Multiple Columns Using := in Data.Table, by Group
Rename Multiple Columns by Names
Conditional Merge/Replacement in R
Increment by 1 For Every Change in Column
How to Order Data by Value Within Ggplot Facets
What Does %≫% Function Mean in R
As.Date With Dates in Format M/D/Y in R
Create Discrete Color Bar With Varying Interval Widths and No Spacing Between Legend Levels
R Shiny - Add Tabpanel to Tabsetpanel Dynamically (With the Use of Renderui)
Creating a Comma Separated Vector
Using the Rjava Package on Win7 64 Bit With R
A Comprehensive Survey of the Types of Things in R; 'Mode' and 'Class' and 'Typeof' Are Insufficient
Idiomatic R Code For Partitioning a Vector by an Index and Performing an Operation on That Partition
Identify Groups of Linked Episodes Which Chain Together
Aggregate a Data Frame Based on Unordered Pairs of Columns
R.Exe, Rcmd.Exe, Rscript.Exe and Rterm.Exe: What's the Difference
Error: '\R' Is an Unrecognized Escape in Character String Starting "C:\R"