loops

Looping through Columns replicating each column fetched six times

给你一囗甜甜゛ 提交于 2021-02-05 07:58:26
问题 I have this data frame where the column names are from v1 to v292. There are 17 observations. I need to iterate over the columns and replicate each column fetched 6 times. For example: v1 v2 v3 v4 1 3 4 6 3 4 3 1 What the output should be x 1 3 1 3 1 3 1 3 1 3 1 3 3 4 3 4 3 4 3 4 3 4 3 4 .. and so on. Please help. Thank you in advance. 回答1: You could use rep data.frame(x = unlist(rep(df, each = 6))) Checking output with each = 2 data.frame(x = unlist(rep(df, each = 2))) # x #1 1 #2 3 #3 1 #4

Loop through XML in Python

瘦欲@ 提交于 2021-02-05 07:57:25
问题 My data set is as following: <?xml version="1.0" encoding="UTF-8"?> <depts xmlns="http://SOMELINK" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" date="2021-01-15"> <dept dept_id="00001" col_two="00001value" col_three="00001false" name = "some_name"> <owners> <currentowner col_four="00001value" col_five="00001value" col_six="00001false" name = "some_name"> <addr col_seven="00001value" col_eight="00001value" col_nine="00001false"/> <

Sample using start and end values within a loop in R

让人想犯罪 __ 提交于 2021-02-05 07:53:36
问题 I am trying to sample between a range of values as part of a larger loop in R. As the loop progresses to each row j , I want to sample a number between the value given in the start column and the value given in the end column, placing that value in the sampled column for that row. The results should look something like this: ID start end sampled a 25 67 44 b 36 97 67 c 23 85 77 d 15 67 52 e 21 52 41 f 43 72 66 g 39 55 49 h 27 62 35 i 11 99 17 j 21 89 66 k 28 65 48 l 44 58 48 m 16 77 22 n 25

Sample using start and end values within a loop in R

拜拜、爱过 提交于 2021-02-05 07:53:12
问题 I am trying to sample between a range of values as part of a larger loop in R. As the loop progresses to each row j , I want to sample a number between the value given in the start column and the value given in the end column, placing that value in the sampled column for that row. The results should look something like this: ID start end sampled a 25 67 44 b 36 97 67 c 23 85 77 d 15 67 52 e 21 52 41 f 43 72 66 g 39 55 49 h 27 62 35 i 11 99 17 j 21 89 66 k 28 65 48 l 44 58 48 m 16 77 22 n 25

Check if an int is prime Java

会有一股神秘感。 提交于 2021-02-05 07:00:36
问题 sorry for the "fix my code" post EDIT: related more to syntax of a for loop than prime numbers, also solved now. My task is to take an int from the console and print out (on separate lines) all the prime numbers from 1 to n inclusive. My method starts at n, checks if its prime, then increments n down by one and loops until n=2. To check if a number is prime I run a loop, checking is the remainder of diving the number by x is equal to zero, with x starting at 2 and stopping at root(n). Now

How to cbind many data frames with a loop?

旧时模样 提交于 2021-02-05 06:50:32
问题 I have 105 data frames with xts, zoo class and II want to combine their 6th columns into a data frame. So, I created a data frame that contains all the data frame names to use it with a 'for' function: mydata <- AAL for (i in 2:105) { k <- top100[i,1] # The first column contains all the data frame names mydata <- cbind(mydata, k) } It's obviously wrong, but I have no idea either how to cbind so many data frames with completely different names (my data frame names are NASDAQ Symbols) nor how

Compare elements of two arrays in less than O(n^2) time?

喜你入骨 提交于 2021-02-05 06:50:12
问题 I have two integer arrays. I need to find out two numbers, one from each array, whose sum is equal to 2. This is very simple in O(n^2) but is there a way to do it faster? 回答1: You can do it in O(N+M) time and O(N) space like this: Put elements of array a into a hash set Walk through array b , and check if hash table contains 2-b[i] Constructing a hash set of N elements takes O(N) time and O(N) space. Checking each of M elements against the hash set takes O(1), for a total of O(N+M) time. 来源:

Find the sum of digits of a number(in c)

梦想与她 提交于 2021-02-04 21:36:37
问题 I need to find the sum of the digits of a number. For example, the sum of the digits of the number 1123 is 1+1+2+3 =7 My idea: 1)User enters and integer 2)I calculate the number of digits in the number(in case above - 4 digits) 3)Than using for loop I divide users number by 10 to the power of 1,2...till the number of digits(not including the last one) and sum the numbers. Here is my code: int main (void) { int result,sum,n; int div = 10,counter = 0,number; printf("Enter the integer:"); scanf(

Sum numbers using loop

故事扮演 提交于 2021-02-04 21:33:57
问题 My objective: write an application that uses while loop to get 20 inputs from a user and displays the sum of all those numbers. I get how to do the while loop but I don't know how to get the sum of all those numbers (because the variable will be the same). Here is what I have so far: Scanner Numb = new Scanner (System.in); int count = 0; while (count<20) { System.out.println("Enter number: "); int numb = Numb.nextInt(); count++; 回答1: use a common variable to store sum int sum = 0; int count =

R: Sum until 0 is reached and then restart

无人久伴 提交于 2021-02-04 21:30:09
问题 Adding on to what's already being said or commented on this post: Cumulative sum until maximum reached, then repeat from zero in the next row I've a similar dataframe which has about 50k+ observations. This dataframe was being read from a csv file and is an outcome of several operations already performed on it. Pasting a sample here: Home Date Time Appliance Run value 679 2 1/21/2017 1:30:00 0 1 0 680 2 1/21/2017 1:45:00 0 1 0 681 2 1/21/2017 2:00:00 0 1 0 682 2 1/21/2017 2:15:00 0 1 0 683 2