data-manipulation

Replace each element equal to zero of a matrix with the corresponding element of the row above

拥有回忆 提交于 2019-12-02 00:18:53
I'm using R. I have a matrix and I want to replace each element of it equal to zero with the corresponding element of the row above. For example, I created the following matrix: AA <- matrix(c(1,2,3,1,4,5,1,0,2), ncol=3, nrow=3) [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 4 0 [3,] 3 5 2 I want to replace 0 with the element AA[1,3]. I would like a function able of doing this for each element of a matrix. We could find the row/column index of elements that are 0 in the matrix ('i1'), then extract the elements that correspond to 1 row above by subtracting one from the row index in 'i1' and replace the

Postgres: convert single row to multiple rows (unpivot)

烂漫一生 提交于 2019-12-01 18:08:17
I have a table: Table_Name: price_list --------------------------------------------------- | id | price_type_a | price_type_b | price_type_c | --------------------------------------------------- | 1 | 1234 | 5678 | 9012 | | 2 | 3456 | 7890 | 1234 | | 3 | 5678 | 9012 | 3456 | --------------------------------------------------- I need a select query in Postgres which gives result like this: --------------------------- | id | price_type | price | --------------------------- | 1 | type_a | 1234 | | 1 | type_b | 5678 | | 1 | type_c | 9012 | | 2 | type_a | 3456 | | 2 | type_b | 7890 | | 2 | type_c |

get first and last values in group – dplyr group_by with last() and first()

懵懂的女人 提交于 2019-12-01 16:53:16
The code below should group the data by year and then create two new columns with the first and last value of each year. library(dplyr) set.seed(123) d <- data.frame( group = rep(1:3, each = 3), year = rep(seq(2000,2002,1),3), value = sample(1:9, r = T)) d %>% group_by(group) %>% mutate( first = dplyr::first(value), last = dplyr::last(value) ) However, it does not work as it should. The expected result would be group year value first last <int> <dbl> <int> <int> <int> 1 1 2000 3 3 4 2 1 2001 8 3 4 3 1 2002 4 3 4 4 2 2000 8 8 1 5 2 2001 9 8 1 6 2 2002 1 8 1 7 3 2000 5 5 5 8 3 2001 9 5 5 9 3

Manupilating previous month data according to current month

南笙酒味 提交于 2019-12-01 14:41:36
I have one table name Prv_Data which contain previous month of report, having Report_Id and Timeline column. Prv_Data --> Report_ID | Timeline ---------------|-------------- 01 | Weekly @Mon 01 | Weekly @Mon 01 | Weekly @Mon 01 | Weekly @Mon 02 | Weekly @Thru 02 | Weekly @Thru 02 | Weekly @Thru 02 | Weekly @Thru 02 | Weekly @Thru I have another table name as Cur_Month which contain current month details. Cur_Month--> Details | Count --------------|-------- First Date | 05/01/2017 Last Date | 05/31/2017 Friday | 4 Monday | 5 Saturday | 4 Sunday | 4 Thursday | 4 Tuesday | 5 Wednesday | 5 Now I

Extract letters from a string in R

我的未来我决定 提交于 2019-12-01 14:31:18
问题 I have a character vector containing variable names such as x <- c("AB.38.2", "GF.40.4", "ABC.34.2") . I want to extract the letters so that I have a character vector now containing only the letters e.g. c("AB", "GF", "ABC") . Because the number of letters varies, I cannot use substring to specify the first and last characters. How can I go about this? 回答1: you can try sub("^([[:alpha:]]*).*", "\\1", x) [1] "AB" "GF" "ABC" 回答2: The previous answers seem more complicated than necessary. This

How to apply same operation to multiple data frames in dplyr-R?

夙愿已清 提交于 2019-12-01 12:55:05
I would like to apply the same operation to multiple data frames in 'R' but cannot get how to deal with this matter. This is an example of pipe operation in dplyr : library(dplyr) iris %>% mutate(Sepal=rowSums(select(.,starts_with("Sepal"))), Length=rowSums(select(.,ends_with("Length"))), Width=rowSums(select(.,ends_with("Width")))) iris2 <- iris iris3 <- iris Could you suggest how to apply the same pipe function to iris , iris2 and isis3 ? I need to use dplyr piping operation. I suppose map function may help but as I have not fully understand its concept, I got errors to apply it. Sample

How to apply same operation to multiple data frames in dplyr-R?

左心房为你撑大大i 提交于 2019-12-01 10:22:29
问题 I would like to apply the same operation to multiple data frames in 'R' but cannot get how to deal with this matter. This is an example of pipe operation in dplyr : library(dplyr) iris %>% mutate(Sepal=rowSums(select(.,starts_with("Sepal"))), Length=rowSums(select(.,ends_with("Length"))), Width=rowSums(select(.,ends_with("Width")))) iris2 <- iris iris3 <- iris Could you suggest how to apply the same pipe function to iris , iris2 and isis3 ? I need to use dplyr piping operation. I suppose map

PHP XMLReader read , edit Node , write XMLWriter

徘徊边缘 提交于 2019-12-01 04:13:08
问题 I have an XML file which is very very large (millions of records). Due to speed and memory constraints I plan to use XMLReader / XMLWriter . I need to read the file, getting one record, change its attribute, and finally save XML again. For testing I created an XML file and write some records into it using these lines: $doc = new XMLWriter(); $doc->openURI($xmlFile); $doc->startDocument('1.0','UTF-8'); $doc->setIndent(4); $doc->startElement('DBOS'); for($r=0;$r<10; $r++){ $doc->startElement(

Normalize (reformat) cross-tab data for Tableau without using Excel

陌路散爱 提交于 2019-12-01 03:02:49
Tableau generally works best when input data is in "normalized" format , rather than cross-tab. This is also referred to as converting from "wide format" to "long format". That is, converting from: To: Tableau provides a "reshaping tool" for Excel users , but if you don't have Excel, you're stuck. So how can you get a spreadsheet into this format, without using Excel? Well, you can use this handy Google Sheets script I made. /* normalizeCrossTab: Converts crosstab format to normalized form. Given columns abcDE, the user puts the cursor somewhere in column D. The result is a new sheet,

Normalize (reformat) cross-tab data for Tableau without using Excel

◇◆丶佛笑我妖孽 提交于 2019-11-30 22:45:47
问题 Tableau generally works best when input data is in "normalized" format, rather than cross-tab. This is also referred to as converting from "wide format" to "long format". That is, converting from: To: Tableau provides a "reshaping tool" for Excel users, but if you don't have Excel, you're stuck. So how can you get a spreadsheet into this format, without using Excel? 回答1: Well, you can use this handy Google Sheets script I made. /* normalizeCrossTab: Converts crosstab format to normalized form