loops

R nested map through columns

前提是你 提交于 2021-02-19 06:18:08
问题 I got a function which was solved here. This function takes a column filled with annotations and another grouping column and propagates the annotation to rows with missing values. f1 <- function(data, group_col, expand_col){ data %>% dplyr::group_by({{group_col}}) %>% dplyr::mutate( {{expand_col}} := dplyr::case_when( !is.na({{expand_col}}) ~ {{expand_col}} , any( !is.na({{expand_col}}) ) & is.na({{expand_col}}) ~ paste(unique(unlist(str_split(na.omit({{expand_col}}), " ")) ), collapse = " ")

R - Add columns to dataframes in list by looping through elements in a vector

谁说胖子不能爱 提交于 2021-02-19 04:14:54
问题 I am working with several datasets that measure the same variables over many years. I am trying to add a year variable to each dataset, but more generally I want to loop through elements in a vector and add each as a new column in a list of dataframes. This question was similar to mine but I want to iteratively add each element in a vector to the corresponding dataframe as a new column: R - New variables over several data frames in a loop Here's sample data: year <- c(1:3) data1 <- data.frame

R - Add columns to dataframes in list by looping through elements in a vector

拟墨画扇 提交于 2021-02-19 04:13:50
问题 I am working with several datasets that measure the same variables over many years. I am trying to add a year variable to each dataset, but more generally I want to loop through elements in a vector and add each as a new column in a list of dataframes. This question was similar to mine but I want to iteratively add each element in a vector to the corresponding dataframe as a new column: R - New variables over several data frames in a loop Here's sample data: year <- c(1:3) data1 <- data.frame

Nested while read loops with fd

混江龙づ霸主 提交于 2021-02-19 04:04:13
问题 I'm trying to read from two different inputs in nested loops without success. I've followed the best answer on this question and also took a look at the file descriptors page of the Advanced Bash-Scripting Guide . Sample script I made to test my problem. #!/bin/bash while read line <&3 ; do echo $line while read _line <&4 ; do echo $_line done 4< "sample-2.txt" done 3< "sample-1.txt" Content of sample-1.txt Foo Foo Content of sample-2.txt Bar Bar Expected output Foo Bar Bar Foo Bar Bar The

MySQL foreach loop

五迷三道 提交于 2021-02-19 02:59:06
问题 I have to iterate each row in my table User in MySQL. I need to create a new row Address for each iteration in User with some conditions described below. I have 3 tables: User: id, stuff, id_person, email Person: id, stuff, id_address Address: id, email I need to create a new row in Address if the User.id_person is NOT NULL and that person.id_address IS NULL. I have to create the row with the same email that User.email. I have to do that for each row in User. I tried to use MySQL cursor's but

aggregate less efficient than loops?

旧街凉风 提交于 2021-02-19 02:48:27
问题 I was trying to do this operation on a big table, to count rows with different combinations of a and b in a data.table X. Y <- aggregate(c ~ a+b,X,length) And it was taking forever (I stopped after 30 min) though RAM usage was still. Then I tried to loop manually through values of b and aggregate only on a (technically still aggregating on b but with a single value of b every time) : sub_agg <- list() unique_bs <- unique(X$b) for (b_it in unique_bs){ sub_agg[[length(sub_agg)+1]] <- aggregate

for/continue in scheme/lisp

旧街凉风 提交于 2021-02-19 01:16:57
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows

for/continue in scheme/lisp

扶醉桌前 提交于 2021-02-19 01:14:03
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows

SAS if statement in do loop

泄露秘密 提交于 2021-02-18 19:30:55
问题 Hi I am trying to write a macro function with do loop and if statement. I think I have messed up the if-then do and do loop and I can't figure out the problem. I have a table of kids information, which contains columns like age, gender, sports, instruments etc,. My original code, which works, looks like this: data old; set new; if sports in ("football","basketball") and age <=7 then type =1; else if sports='swimming' then type=2; if special_kid=. then do; if piano ^=. and piano_1 ^=. then do;

Allocation of space for local variables in loops

对着背影说爱祢 提交于 2021-02-18 07:41:44
问题 (true or false) The space for a local variable that is declared in the body of the loop is allocated whenever the loop body is executed and deallocated when the body finishes. The answer to this question is false. But why? 回答1: The statement is false because local variable space is not allocated and deallocated. It exists on the stack and is reserved when the method is entered. To see how stack space is used, write a small test program with: public static void test() { { int a = 1; long b = 2