reshape

Casting a Date matrix?

陌路散爱 提交于 2019-12-11 13:48:54
问题 I have a data frame containing the following fields: a, b, c. a and b are identifiers and c is a Date. Not all identifier combinations have a date. There are some duplicate (a,b)s in the data. I only need the last c. I want to create a table where the levels of a form the rows and the levels of b form the columns. If there is a c matching the levels of a and b, it should end up in the corresponding cell (t[a,b] = c). (I want to cluster the events with the table as a basis for a distance

R: Reshape from wide to long, can't get order right

萝らか妹 提交于 2019-12-11 12:23:38
问题 I reshaped my data from wide to long, but I can't get the order right: data <- as.data.frame(matrix(c(rep(1:5),0,0,0,5,1,0,0,0,5,0),5,3)) colnames(data) <- c("id", "x1.a", "x3.a") print(data) # id x1.a x3.a # 1 1 0 0 # 2 2 0 0 # 3 3 0 0 # 4 4 5 5 # 5 5 1 0 reshaped <- reshape(data, varying = 2:3, v.names = "x.a", times = c(1,3), timevar = "time", idvar = "id", direction = "long") print(reshaped) # id time x.a # 1.1 1 1 0 # 2.1 2 1 0 # 3.1 3 1 0 # 4.1 4 1 5 # 5.1 5 1 1 # 1.3 1 3 0 # 2.3 2 3 0

Converting specific cells of data frame to table in R

℡╲_俬逩灬. 提交于 2019-12-11 10:23:40
问题 I have a data frame (read from RDS file) with 140 variables. I have subsetted 3 of them. But the subset has only one row with three column variables. I have to present it as a table and make a bar chart too. The subset data frame looks like this. HomeCondn_Good HomeCondn_Livabl HomeCondn_Dilapdtd (dbl) (dbl) (dbl) 1 65.9 29.7 4.3 The reproducible example is as follows: structure(list(HomeCondn_Good = 65.9, HomeCondn_Livabl = 29.7, HomeCondn_Dilapdtd = 4.3), .Names = c("HomeCondn_Good",

Forcing unique values before casting (pivoting) in R

旧巷老猫 提交于 2019-12-11 09:29:41
问题 I have a data frame as follows Identifier V1 Location V2 1 12 A 21 1 12 B 24 2 20 B 15 2 20 C 18 2 20 B 23 3 43 A 10 3 43 B 17 3 43 A 18 3 43 B 20 3 43 C 25 3 43 A 30 I’d like to re-cast it with a single row for each Identifier and one column for each value in the current location column. I don’t care about the data in V1 but I need the data in V2 and these will become the values in the new columns. Note that for the Location column there are repeated values for Identifiers 2 and 3. I ASSUME

Turn long dataset of classes taken into wide dataset where variables are dummy code for each class

妖精的绣舞 提交于 2019-12-11 08:00:01
问题 Say I have a dataset where rows are classes people took: attendance <- data.frame(id = c(1, 1, 1, 2, 2), class = c("Math", "English", "Math", "Reading", "Math")) I.e., id class 1 1 "Math" 2 1 "English" 3 1 "Math" 4 2 "Reading" 5 2 "Math" And I want to create a new dataset where rows are ids and the variables are class names, like this: class.names <- names(table(attendance$class)) attedance2 <- matrix(nrow=length(table(attendance$id)), ncol=length(class.names)) colnames(attedance2) <- class

NumPy Array Reshaped but how to change axis for pooling?

感情迁移 提交于 2019-12-11 07:58:55
问题 I have a 8x8 matrix as follows: [[ 0.3 0.3 0.3 0.3 0.3 0.5 0.1 -0.1] [ 0.1 0.1 -0.1 0.3 0.3 -0.1 -0.1 -0.5] [-0.1 0.1 0.3 -0.1 0.3 -0.1 -0.1 -0.1] [-0.1 0.1 0.5 0.3 -0.3 -0.1 -0.3 -0.1] [ 0.5 0.1 -0.1 0.1 -0.1 -0.1 -0.3 -0.5] [ 0.1 -0.1 -0.3 -0.5 -0.5 -0.1 -0.1 -0.3] [-0.5 -0.3 -0.3 -0.3 -0.1 -0.5 -0.1 -0.3] [-0.3 -0.3 -0.3 -0.3 -0.1 -0.1 -0.5 -0.3]] My window is 2x2. What I am trying to do is get four numbers together (up and down numbers) for pooling. Sample output looks like this: [[0.3 0

R Frequency table for multiselect survey question across several columns

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:46:08
问题 I want to do a fairly common analysis of survey questions in R, but am stuck in the middle. Imagine a survey where you are asked to answer which brands do you associate with certain features (e.g. "brands" could be PlayStation, XBox..., and features could be "speed", "graphics"... where each brand can be checked on several features aka mulit-select). E.g. sth. like this here: https://www.harvestyourdata.com/fileadmin/images/question-type-screenshots/Grid-multi-select.jpg You often refer to

r- reshape table grouped by 3 variables (2 in rows) [duplicate]

社会主义新天地 提交于 2019-12-11 07:14:57
问题 This question already has answers here : Faster ways to calculate frequencies and cast from long to wide (4 answers) Closed last year . I need to reshape to wide format the following table: > data dia cli llam elegidos cumllam 1 1-11 a 1 1 1 2 2-11 a 2 1 3 3 1-11 b 2 1 2 4 2-11 b 1 1 3 5 2-11 c 1 0 1 I need to have days in rows and cumllam in columns and the quantity of clients as a value. I wrote: library(reshape2) my.f <- function (v) {if (length(v) == 0) 0 else length(v)} series<-data

Reshaping multiple groups of columns in a data frame from wide to long

无人久伴 提交于 2019-12-11 06:59:36
问题 I am working with air-quality data. I tried to reshape the data frame from wide to long using melt function. Here is the data: Elev stands for Elevation , Obs for observation and US3, DK1, DE1 are models, where lm and ul represents first and third quantiles. Elev Obs lm ul US3 lm ul DK1 lm ul 1 0 37.74289 34.33422 41.27840 38.82037 35.35241 42.30042 49.31111 45.00134 53.90968 2 100 38.14076 34.71842 41.36560 39.82727 36.49086 43.22209 50.46545 45.79068 55.44664 3 250 39.31056 35.98180 42

How to assign number of repeats to dataframe based on elements of an identifying vector in R?

…衆ロ難τιáo~ 提交于 2019-12-11 06:24:59
问题 I have a dataframe with individuals assigned a text id that concatenates a place-name with a personal id (see data, below). Ultimately, I need to do a transformation of the data set from "long" to "wide" (e.g., using "reshape") so that each individual comprises one row, only. In order to do that, I need to assign a "time" variable that reshape can use to identify time-varying covariates, etc. I have (probably bad) code to do this for individuals that repeat up to two times, but need to be