apply

Subtract matrix of n,k dimensions from array of matrices of n,k dimensions

有些话、适合烂在心里 提交于 2019-12-12 10:39:25
问题 If I have an array A A <- array(0, c(4, 3, 5)) for(i in 1:5) { set.seed(i) A[, , i] <- matrix(rnorm(12), 4, 3) } and if I have matrix B set.seed(6) B <- matrix(rnorm(12), 4, 3) The code to subtract B from the each matrix of the array A would be: d<-array(0, c(4,3,5)) for(i in 1:5){ d[,,i]<-A[,,i]-B } However, what would be the code to perform the same calculation using a function from "apply" family? 回答1: This is what sweep is for. sweep(A, 1:2, B) 回答2: Maybe not very intuitive: A[] <- apply

R: Webscraping a list of URLs to get a DataFrame

心不动则不痛 提交于 2019-12-12 09:23:23
问题 I can see the correct data, but cannot put it on a Data Frame (It appears as a list of elements). I think the problem is my understanding of the apply family functions. Any hint is welcome. Here is a similar question, but I think it is better to post mine as it contains more details: Webscraping content across multiple pages using rvest package library(rvest) library(lubridate) library(dplyr) urls <- list("http://simple.ripley.com.pe/tv-y-video/televisores/ver-todo-tv", "http://simple.ripley

apply a function on rolling window in Dataframe where whole dataframe is passed to function

假装没事ソ 提交于 2019-12-12 08:55:31
问题 I have a dataframe of 5 columns indexed by YearMo: yearmo = np.repeat(np.arange(2000, 2010) * 100, 12) + [x for x in range(1,13)] * 10 rates = pd.DataFrame(data=np.random.random(120, 5)), index=pd.Series(data=yearmo, name='YearMo'), columns=['A', 'B','C', 'D', 'E']) rates.head() YearMo A B C D E 200411 0.237696 0.341937 0.258713 0.569689 0.470776 200412 0.601713 0.313006 0.221821 0.720162 0.889891 200501 0.024379 0.761315 0.225032 0.293682 0.302431 200502 0.996778 0.388783 0.026448 0.056188 0

dendrapply Error: C stack usage is too close to the limit

家住魔仙堡 提交于 2019-12-12 06:22:11
问题 I'm using R 's dendrapply this way: dendrapply(dendro, function(n) utils::str(attributes(n))) where dendro is a 'dendrogram' with 2 branches and 5902 members total, at height 2 . After a while that it's running it crashes with this error: Error: C stack usage 7971524 is too close to the limit Any idea? 回答1: It looks like you have an infinite recursion situation which has arisen because the nodes are not returned in your function. If you are just looking to print the structure of the

Vectorize/Accelerate looping through a struct of struct in Matlab?

 ̄綄美尐妖づ 提交于 2019-12-12 05:47:39
问题 I am looking for something similar to sapply function in R in Matlab. I have the current issue: I have a large struct of size 1000, each one inside is a struct, that is, I have a struct of struct . Each substruct are in the same style, that is, the same fields. I am using a function to do something on each substruct The code looks like: for i =1:length(mainStruct) disp(i); result(i) = myfunction(mainStruct(i).field(1:1000)); end In the above, myfunction is just a function, mainStruct is the

Exit a function within *apply

◇◆丶佛笑我妖孽 提交于 2019-12-12 05:39:52
问题 sapply and replicate (etc.) run a specified number of times. Putting sapply(1:N, function(n){expr}) will execute expr N times. Supposing I wanted sapply to stop after m runs. Is this possible without making an error? break doesn't work, and a for or while loop would be too slow in my context. Something akin to: sapply(1:N, function(n){ #some expression if(identical(n, m)) break }) except that break doesn't work. What I'm trying to do: Creating a function to read in large (binary) data files

Python Pandas Series combine the rows

白昼怎懂夜的黑 提交于 2019-12-12 05:24:12
问题 My pd.series looks like this: df.head() 0 status parentName name describe parent... 1 status parentName name describe parent... 2 status parentName name describe parent... 3 status parentName name describe parent... 4 status parentName name describe parent... Name: destinationurl, dtype: object and each row is a dataframe, which looks like this: status parentName name describe parentDescribe parentEnName parentID id enName 0 0 IT 电子邮箱 提供电子邮箱服务的站点。 Information Technology 25 144 Email Now I

Multiply recursiverly in r

自作多情 提交于 2019-12-12 05:06:43
问题 Having the following matrix and vector. x<-matrix(c(1,4,7, 2,5,8, 3,6,9), nrow = 3) w <- c(1,1,1) res <- c() What is the best way to multiply recursiverly till obtain a desire sum of the results as exemplified: res[1]<-w %*%x[1,] res[2]<-w %*%x[2,] res[3]<-w %*%x[3,] res[4]<-w %*%x[1,] res[5]<-w %*%x[2,] sum(res)>1000 #Multiply recursiverly till the sum of the results sum(res) goes further than 1000. 回答1: Here is how to do it recursively : f <- function(x, w, res){ if (sum(res)>1000) return

SharedPreferences.apply() and ANR application

落花浮王杯 提交于 2019-12-12 04:54:52
问题 I used to SharedPreferences.apply() method. When this method is called very often, then it hangs the application. Commit() method is very slow, but is working properly. You can get the ANR in my example. Fold and unfold the activity! public class Main extends Activity { @Override public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.main); new Thread(new Runnable() { @Override public void run() { while(true) { SharedPreferences.Editor ed = getEditor(); ed.putString

Apply User Defined Functions

可紊 提交于 2019-12-12 04:36:23
问题 I have a user-defined function. funct<-function(object,state){ Init<-matrix(0,nrow=1,ncol=length(object)) colnames(Init)<-unique(object) Init[,which(colnames(Init)==state)]<-Init[,which(colnames(Init)==state)]+1 return(Init) } Show the function output letters1<-letters[1:5] funct(letters1,"b") I have a data frame, and apply my function on it to combine it by rows by using apply family. dat<-data.frame(letters1=c("a","b")) My try on it mapply(funct,unique(dat$letters1),dat$letters1) Desired