This question already has an answer here:
multiply multiple column and find sum of each column for multiple values
(1 answer)
Closed 9 years ago.
this is my code
here the paste function works for only two combination.i need the same code in a loop for more than two combinations at the same time.
i<-2
while (i<=10)
{
results<-data.frame()
results<- t(apply(data,1,function(x) combn(x,i,prod)))
comb <- combn(colnames(data),i)
colnames(results) <- apply(comb,i,function(x) paste(x[1],x[2]))
i<-i+1
}
now i get the two combination like
V1V2, V1V3,V1V4,....
now i want
v1v2v3, v1v2v4, ...
in paste function.
comb <- combn(colnames(data),v)
colnames(results) <- apply(comb,2,function(rows) paste0(rows, collapse = ""))
insted of paste use paste0
#henrik and #chargaff
Related
This question already has answers here:
Pasting two vectors with combinations of all vectors' elements
(8 answers)
Closed 1 year ago.
I have two lists, which I want to use to create a new list that is a product of the length of the two lists. I have code that does this with for loops, but I would like to do it in one line with something vectorized like lapply or map2. Thanks!
years <- c(1999, 2000, 2001)
file <- list("tmin", "tmax")
var <- vector()
for(ys in years){
#determine which days need to be extracted
for(f in file){
var <- append(var, paste0(f, ".", ys, ".nc"))
}
}
Unless this is a simplified example and something more is needed to scale, this will produce the same output:
paste(file, rep(years, each = length(file)), 'nc', sep = '.')
[1] "tmin.1999.nc" "tmax.1999.nc" "tmin.2000.nc" "tmax.2000.nc" "tmin.2001.nc" "tmax.2001.nc"
This question already has answers here:
How to convert certain columns only to numeric?
(4 answers)
Make a list from ls(pattern="") [R]
(1 answer)
Closed 2 years ago.
I have a number of x dataframes (depending on previous operation). The names of the dataframes are stored in a different vector:
> list.industries
[1] "misc" "machinery" "electronics" "drugs" "chemicals"
Now, I want to set every column after the 4th as numeric. As the number of created dataframes and, therefore, the names change, I want to ask, if there is any way to do it automatically.
I tried:
for (i in 1:length(list.industries)) {
paste0(list.industries) <- lapply(paste0(list.industries)[,4:ncol(paste0(list.industries))] , as.numeric)
}
Where the function places automatically the name of the dataframe from the vector list.industries to set it as numeric.
Is there any way, how I can place the name of a dataframe as a variable from a vector?
Thanks!
You can use mget to get data as a named list, turn every columns after 4th as numeric and return the dataframe back.
new_data <- lapply(mget(list.industries), function(x) {
x[, 4:ncol(x)] <- lapply(x[, 4:ncol(x)], as.numeric)
x
})
new_data would have list of dataframes, if you want the changes to be reflected in the orignal dataframe use list2env.
list2env(new_data, .GlobalEnv)
You could use this fragment (untested):
one_df <- function(x) {
dat <- get(x)
for (i in seq(4, ncol(dat))) dat[,i] <- as.numeric(dat[,i])
return(dat)
}
ans <- lapply(list.industries, one_df)
So in short: you are looking for get.
This question already has answers here:
How to extract certain columns from a list of data frames
(3 answers)
Closed 2 years ago.
so x is a vector. i am trying to print the first col of df's name's saved in the vector. so far I have tried the below but they don't seem to work.
x = (c('Ethereum,another Df..., another DF...,'))
for (i in x){
print(i[,1])
}
sapply(toString(Ethereum), function(i) print(i[1]))
You can try this
x <- c('Ethereum','anotherDf',...)
for (i in x){
print(get(i)[,1])
}
You can use mget to get data in a list and using lapply extract the first column of each dataframe in the list.
data <- lapply(mget(x), `[`, 1)
#Use `[[` to get it as vector.
#data <- lapply(mget(x), `[[`, 1)
Similar solution using purrr::map :
data <- purrr::map(mget(x), `[`, 1)
This question already has answers here:
Get the index of the values of one vector in another?
(3 answers)
Closed 5 years ago.
I like to give all values which are in the dat vector a corresponding value which I define in another vector (new_value). I already managed to do somehow, but I like to have a solution without loop:
dat <- c("a","c","b","a","a","c","a","b","b","a")
old_value <- names(table(dat))
new_value <- 1:length(old_value)
new_dat <- rep(NA, length(dat))
for(z in 1:length(old_value)){
new_dat[dat==old_value[z]] <- c(1:length(new_value))[z]
}
new_dat
I don't want to use additional libraries. Please only base solutions.
We can use match
new_dat <- new_value[match(dat, old_value)]
For the current example, even
match(dat, old_value)
should work
This question already has answers here:
Convert a list to a data frame
(26 answers)
Closed 8 years ago.
Apologies in advance if this is a duplicate question, but I can't find anything on this in the stack overflow archives.
I wrote a function that returns a zoo object (makeTrace1). In a second function (make2DHist), I call this function many times and so generate a list of dataframes (converted from the returned zoo objects). I would then like to rbind all these dataframes in my list together. I can rbind specific elements together so I could do this with a for loop, but is there a vectorized expression to rbind all the dataframes in the list together?
Here's my code:
make2DHist <- function()
{
v = list()
times=4
for(i in 1:times)
{
vv = makeTrace1()
v[[i]] = data.frame(Date=time(vv), vv, check.names = FALSE, row.names=NULL)
}
hhh = rbind(v[[1]], v[[2]]) <-----this works
hhh2 = rbind(v[c(1:4)]) <-this does not work
}
m= make2DHist()
> x <- do.call(rbind, your_list_of_matrices_or_data_frames_here)