add a data frame to a constructed name - r

I have this
for(i in 1:10)
and within it, I have a data frame:
e.g.
df<-1:100
and I want to assign the dataframe to a specific name which I want to create
something like: (not that it works)
paste("name", variable[i])<- df
Edit:
How would I then go about accessing those constructed values in another loop (assuming i've used assign)
datalist <- paste("a",1:100,sep="")
for (i in 1:length(datalist)){
}

I suggest assign, as illustrated here:
for(i in 1:100){
df <- data.frame(x = rnorm(10),y = rpois(10,10))
assign(paste('df',i,sep=''),df)
}

You could store the output of your loop in a list:
set.seed(10)
x = list()
for(i in 1:10){
x[[i]] <- data.frame(x = rnorm(100), y = rnorm(100))
}
Then x will be a list of length 10 and each element of the list will be of dim c(100, 2)
> length(x)
[1] 10
> dim(x[[1]])
[1] 100 2
>
Of course you could name the elements of the list, as well:
names(x) = letters[1:10]
x[["a"]]
x y
1 0.01874617 -0.76180434
2 -0.18425254 0.41937541
3 -1.37133055 -1.03994336
4 -0.59916772 0.71157397
5 0.29454513 -0.63321301
6 0.38979430 0.56317466
...

Related

R assign along a vector

I have an ini-file, read as a list by R (in the example l). Now I want to add further sub-lists along a vector (m) and assign always the same constant to them. My attempt so far:
l <- list("A")
m <- letters[1:5]
n <- 5
for (i in 1:5){
assign(paste0("l$A$",m[i]), n)
}
# which does not work
# example of the desired outcome:
> l$A$e
[1] 5
I don't think that I have fully understood how lists work yet...
Try
L[["A"]][m] <- n
L$A$e
# [1] 5
Data:
L <- list(A = list())
m <- letters[1:5]
n <- 5

Use paste0 to create multiple object names with a for loop

I would like to create multiple object names with a for loop. I have tried the following which fails horribly:
somevar_1 = c(1,2,3)
somevar_2 = c(4,5,6)
somevar_3 = c(7,8,9)
for (n in length(1:3)) {
x <- as.name(paste0("somevar_",[i]))
x[2]
}
The desired result is x being somevar_1, somevar_2, somevar_3 for the respective iterations, and x[2] being 2, 5 and 8 respectively.
How should I do this?
somevar_1 = c(1,2,3)
somevar_2 = c(4,5,6)
somevar_3 = c(7,8,9)
for (n in 1:3) {
x <- get(paste0("somevar_", n))
print(x[2])
}
Result
[1] 2
[1] 5
[1] 8
We can use mget to get all the required objects in a list and use sapply to subset 2nd element from each of them.
sapply(mget(paste0("somevar_", 1:3)), `[`, 2)
#somevar_1 somevar_2 somevar_3
# 2 5 8

R connect string and dynamic variables

some other languages have this:
i=1
x&i=3
Then you will get a variable x1=3
How to realize this in R?
please don't use assign(paste0('x',1),3).
Because I want to iterate i, for example:
x1=c()
for(i in 1:100){
x1=c(x1,2*i)}
And I want x1,x2...xn. assign(paste) only generates variables once and doesn't have adding functions.
So the grammar x&i is the core problem.
Thanks for help.
Try this:
e <- .GlobalEnv
i <- 1
xi.name <- paste0("x", i)
# assign
e[[xi.name]] <- 3
# add
e[[xi.name]] <- e[[xi.name]] + 1
# display
e[[xi.name]]
## [1] 4
or using assign and get the above could be done like this:
i <- 1
xi.name <- paste0("x", i)
# assign
assign(xi.name, 3)
# add
assign(xi.name, get(xi.name) + 1)
# display
get(xi.name)
## [1] 4
Note that normally one does not generate dynamic variables but rather puts them into a list.
L <- list()
i <- 1
xi.name <- paste0("x", i)
# assign
L[[xi.name]] <- 3
# add
L[[xi.name]] <- L[[xi.name]] + 1
# display
L[[xi.name]]
## [1] 4
or simply:
L <- list()
i <- 1
# assign
L[[i]] <- 3
# add
L[[i]] <- L[[i]] + 1
# display
L[[i]]
## [1] 4
Note
e <- .GlobalEnv
i <- 1
xi.name <- paste0("x", i)
x1 <- 3
e[[xi.name]] <- c(e[[xi.name]], 99)
x1
## [1] 3 99
e <- .GlobalEnv
i <- 1
xi.name <- paste0("x", i)
x1 <- 3
assign(xi.name, c(get(xi.name), 99))
x1
## [1] 3 99

Trouble applying function to data frame

Toy example:
> myfn = function(a,x){sum(a*x)}
> myfn(a=2, x=c(1,2,3))
[1] 12
Good so far. Now:
> df = data.frame(a=c(4,5))
> df$ans = myfn(a=df$a, x=c(1,2,3))
Warning message:
In a * x : longer object length is not a multiple of shorter object length
> df
a ans
1 4 26
2 5 26
What I want to happen is that for the first row, it is as if I called myfn(a=4, x=c(1,2,3), giving an answer of 24, and for the second row, it is as if I called myfn(a=5, x=c(1,2,3) giving an answer of 30. How do I do this? Thank you.
EDIT: slightly more complex version. Now suppose that the function is
myfn = function(a,b, x){sum((a+b)*x)}
and that I have the data frame
df = data.frame(a=c(4,5), b=c(6,7), c=c(9,9))
I want to create df$ans such that, for the first row it is as if I called myfn(a=4, b=6, x=c(1,2,3) and for the second for it is as if I called myfn(a=5, b=7, x=c(1,2,3), that is, use df$x for a, df$y for b, and ignore df$z.
Something like this would work:
myfn = function(a,x){
return(sum(a*x))
}
df <- data.frame(a=c(4,5))
df$ans <- apply(df, 1, myfn, x = c(1,2,3))
df$ans
a ans
1 4 24
2 5 30
** Edited Based On User Edit **
df = data.frame(a=c(4,5), b=c(6,7), c=c(9,9))
df$ans <- apply(df[, c("a", "b")], 1, function(y) sum((y['a']+y['b'])*c(1,2,3)))
a b c ans
1 4 6 9 60
2 5 7 9 72
There are several ways this can be done, each with it's own charms. If you don't want to modify the function I would just do
mapply(myfn, df$x, df$y, MoreArgs = list(x = 1:3))
Alternatively, you can bake the iteration right into the function, e.g,
myfn = function(a,b, x){
sapply(a+b, function(ab) {
sum(ab*x)
})
}
myfn(df$x, df$y, 1:3)
That's probably the way I would do it.

create and name NA indicator variables for a large data frame

Okay, I'm close. Everything works but the last loop for compound where I get hung up on a data type issue. Copy and run to your heart's content.
x <- c(1:12)
dim(x) <- c(3,4)
x[2,2] <- NA
x[3,3] <- NA
colnames(x) <- c("A","B","C","D")
x
newframe <- data.frame(matrix(0, ncol = 4, nrow = 3))
for (i in 1:3)
for (j in 1:4)
{ newframe[i,j] <- (1 -1*(is.na(x[i,j]))) }
newframe <- as.matrix((newframe))
newframe
compound <- data.frame(matrix(0, ncol = 4, nrow = 3))
for (i in 1:3)
for (j in 1:4 )
{ compound[i,j] <- (as.numeric(x[i,j])*(as.numeric(newframe[i,j])))
}
compound
I'm trying to create an indicator variable for null instances and use it to create a compound variable that will zero out the original variable when null and flash the indicator.
Create indicator var's for missing instances and zero out or impute values for NA instances in original data:
# create data
x <- c(1:12)
dim(x) <- c(3,4)
x[2,2] <- NA
x[3,3] <- NA
x
# create data frame for indicator var's
newframe <- 1*(is.na(x))
newframe
class(newframe)
# zero out NAs in data, or alternatively replaced with imputed values
x[is.na(x)] <- 0
# create data frame for original data and indicator var's
newdata <- cbind(x, newframe)
newdata
Copy and run.
Is this what you're looking for ?
compound <- x
compound[is.na(x)] <- 0
compound
A B C D
[1,] 1 4 7 10
[2,] 2 0 8 11
[3,] 3 6 0 12

Resources