logo
down
shadow

transposing data by parts


transposing data by parts

By : Chemical Engineer
Date : November 19 2020, 03:01 PM
will be helpful for those in need I would like to transpose this data: , awk to the rescue!
code :
$ awk '{k=$1 FS $2; a[k]=a[k] FS $NF} 
   END {for(k in a) print k a[k]}' file | sort

ZA 1 A_A A_T A_A A_T
ZA 10 A_A A_A A_A A_A


Share : facebook icon twitter icon
Best way to select parts certain parts of data in a string that changes in size

Best way to select parts certain parts of data in a string that changes in size


By : Chronus Du
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You could simply use String.split(), first to tokenize the whitespace and then to tokenize on your key/value separator (colon in this case):
code :
String line = "id:1234   alert:a-b   up:12.3 down:12.3";
// first split the line by whitespace
String[] keyValues = line.split("\\s+");

for (String keyValueString : keyValues) {
    String[] keyValue = keyValueString.split(":");
    // TODO might want to check for bad data, that we have 2 values
    System.out.println(String.format("Key: %-10s Value: %-10s", keyValue[0], keyValue[1]));
}
Key: id         Value: 1234      
Key: alert      Value: a-b       
Key: up         Value: 12.3      
Key: down       Value: 12.3 
How to manipulate long-table data for correlations, using dplyr, but without transposing data to wide format

How to manipulate long-table data for correlations, using dplyr, but without transposing data to wide format


By : Darryl Weatherly
Date : March 29 2020, 07:55 AM
it should still fix some issue We can try either dplyr or data.table methods. Assuming that we want to find the correlation for the 'var' columns, we create an index of the columns using grep ('nm1'), and a custom function (f1). We use Map within do for getting the cor of each 'var' column. We use the 'var' columns as first input i.e. 'x' and 'session' as the 'y'.
code :
nm1 <- grep('^var', names(data))
f1 <- function(x,y) cor(x[y=='A'], x[y=='B']) 
library(dplyr)
data %>% 
      do({data.frame(Map(f1, .[nm1], list(.$session)))})
#   var.1 var.2
#1    -1     1
library(data.table)
setDT(data)[, Map(f1, .SD[, nm1, with=FALSE], list(session)) ]
#   var.1 var.2
#1:    -1     1
mapply(f1, data[nm1], list(data$session))
# var.1 var.2 
# -1     1 
 dW <- dcast(setDT(data), id~session, value.var=c('var.1', 'var.2'))[, id:=NULL]
 dW
 #   var.1_A var.1_B var.2_A var.2_B
 #1:     1.1     1.9      11      12
 #2:     1.2     1.4      15      14
 m1 <- as.matrix(dW)
 diag(cor(m1[,c(TRUE, FALSE)], m1[,c(FALSE, TRUE)]))
 #[1] -1  1
data <- structure(list(id = c(1L, 1L, 2L, 2L), var.1 = c(1.1, 1.9, 1.2, 
1.4), var.2 = c(11L, 12L, 15L, 14L), session = c("A", "B", "A", 
"B")), .Names = c("id", "var.1", "var.2", "session"), 
class = "data.frame", row.names = c(NA, -4L))
Transposing Data SQL

Transposing Data SQL


By : Britney
Date : March 29 2020, 07:55 AM
I wish this help you The data looks similar to this: , You can use a crosstab query, though a bit convoluted it is:
code :
TRANSFORM 
    Sum(TableUnit.Charge) AS SumOfCharge
SELECT 
    S.Unit, 
    S.Floorplan, 
    S.SqFt, 
    S.Name, 
    S.Amenity
FROM 
    TableUnit, 
        (SELECT 
            Q.Id, 
            Val(DMax("Id","TableUnit","Id<=" & Q.[Id] & " And Unit Is Not Null")) AS ParentId
        FROM TableUnit As Q) AS T, 
        (SELECT 
            TableUnit.Id, 
            TableUnit.Unit, 
            TableUnit.Floorplan, 
            TableUnit.SqFt, 
            TableUnit.Name, 
            TableUnit.Amenity
        FROM 
            TableUnit
        WHERE 
            TableUnit.Unit Is Not Null) AS S
WHERE 
    TableUnit.Id=[T].[Id] 
    AND 
    T.ParentId)=[S].[Id]
GROUP BY 
    T.ParentId, 
    S.Unit, 
    S.Floorplan, 
    S.SqFt, 
    S.Name, 
    S.Amenity
PIVOT 
    TableUnit.Amenity In
        ("Garage","Pest","Trash","PetRent","Storage","Rent");
Transposing parts of a data frame to individual columns

Transposing parts of a data frame to individual columns


By : user7081100
Date : March 29 2020, 07:55 AM
this one helps. Even if this is a duplicate, I didn't see the following answer, so...start with the original data:
code :
df <- data.frame( A = c("reg","val1","val2","reg","val1","val2","val3","reg","val1","reg","val3","reg","val2","val4"),
                  B = c(12345, 1, 0, 45678, 0, 0, 1, 97654, 1, 567834, 1, 567845, 0, 1))
install.packages("tidyverse")
library(tidyverse)
df1 <- df %>% 
          mutate(dummy = cumsum(A=="reg")) %>%
          group_by(dummy) %>%
          nest() %>%
          mutate(data = map(data, ~spread(.x, A, B))) %>%
          unnest() %>%
          select(-dummy)
     reg  val1  val2  val3  val4
1  12345     1     0    NA    NA
2  45678     0     0     1    NA
3  97654     1    NA    NA    NA
4 567834    NA    NA     1    NA
5 567845    NA     0    NA     1
df1[is.na(df1)] <- 0

     reg  val1  val2  val3  val4
1  12345     1     0     0     0
2  45678     0     0     1     0
3  97654     1     0     0     0
4 567834     0     0     1     0
5 567845     0     0     0     1
Transposing values in data frame based on data in two columns

Transposing values in data frame based on data in two columns


By : user3721537
Date : March 29 2020, 07:55 AM
I wish this help you I have the following data frame. How can I form a new data frame/matrix where each row comprises the expectation values of each categorical variable in the ID column and have these values taken in ascending order of time? , Here's a simple way:
code :
library(data.table)

df[order(Time),list(list(Expectation)),.(ID)]

           ID             V1
1:        cat      1,1,1,1,1
2:        dog      2,2,3,6,7
3: guinea_pig  7,15,16,10,10
shadow
Privacy Policy - Terms - Contact Us © voile276.org