Skip to content
Prev 372617 / 398498 Next

Dynamic reference, right-hand side of function

Loops are not evil, and no-one in this thread said they are. But I 
believe your failure to provide a reproducible example is creating 
confusion, since you may be using words that mean one thing to you and 
something else to the readers here.

################################
# A reproducible example includes a tiny set of sample data
# Since we cannot reproducibly refer to filenames (your directories
# and files in them are unlikely to be like mine), I will
# use a little trick to read from data in the example:

dta <- read.csv( text=
"1.1,3.0,5,7.4,4,2.2,0
", header=FALSE)
str(dta)
#> 'data.frame':    1 obs. of  7 variables:
#>  $ V1: num 1.1
#>  $ V2: num 3
#>  $ V3: int 5
#>  $ V4: num 7.4
#>  $ V5: int 4
#>  $ V6: num 2.2
#>  $ V7: int 0

# note that I did not use "data" as the name because
# there is a commonly-used function by that name in R
# that could be confused with your variable

# if you have your object already in memory, you can use the
# dput function to create R code that will re-create it in our
# working environments.

dput( dta )
#> structure(list(V1 = 1.1, V2 = 3, V3 = 5L, V4 = 7.4, V5 = 4L,
#>     V6 = 2.2, V7 = 0L), .Names = c("V1", "V2", "V3", "V4", "V5",
#> "V6", "V7"), class = "data.frame", row.names = c(NA, -1L))

# which you can put a variable name in front of in your example code:

dtasample <- structure(list( V1 = 1.1, V2 = 3, V3 = 5L
, V4 = 7.4, V5 = 4L, V6 = 2.2, V7 = 0L ) , .Names = c( "V1"
, "V2", "V3", "V4", "V5", "V6", "V7" ), class = "data.frame"
, row.names = c(NA, -1L) )

# and starting with that line you can make a self-contained
# (reproducible) example for us to investigate your problem with

# Note that reading a single row of data into R usually gets
# a data frame, which looks like a matrix but is not a matrix.
# Read the Introduction to R about these two types carefully.
# Each column in a data frame can have a different type of data,
# but in a vector or a matrix all rows and columns must be of
# the same type.

dtam <- as.matrix( dta )

# If you have any values that R cannot clearly identify as numeric
# or integer, then the next most general type of variable is
# character... and that is often something that trips up newbies,
# though I have no evidence that you have any non-numeric columns
# in your data frames.

dtax <- as.vector( dta )
str(dtax)
#> 'data.frame':    1 obs. of  7 variables:
#>  $ V1: num 1.1
#>  $ V2: num 3
#>  $ V3: int 5
#>  $ V4: num 7.4
#>  $ V5: int 4
#>  $ V6: num 2.2
#>  $ V7: int 0

# This actually makes no change to dta, because a data frame is already
# a list of columns, and lists are just vectors that can hold different
# types of variables, so dta is already a kind of vector.

dtan <- as.numeric( dta )
str(dtan)
#>  num [1:7] 1.1 3 5 7.4 4 2.2 0

# I suspect this is what you are trying to accomplish... but really,
# if we had an example of the data you are working with, we would
# already know.
################################

Some more explanations of reproducibility [1][2][3]

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html (read the 
vignette)
On Mon, 4 Dec 2017, Love Bohman wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k