reshape

Reshaping from long to wide with some missing data (NA's) on time invariant variables

旧街凉风 提交于 2020-01-21 10:29:18
问题 When using stats:::reshape() from base to convert data from long to wide format, for any variables designated as time invariant, reshape just takes the first observed value, and if the variable is actually varying in some way, outputs a warning. In my case, I have missing data on variables I would like to designate as time invariant, but since I have this data at other time points, I would like the value from those time points to be used rather than the NA which is first observed. testdata <-

How to expand a large dataframe in R

落爺英雄遲暮 提交于 2020-01-19 15:05:48
问题 I have a dataframe df <- data.frame( id = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 4), date = c("1985-06-19", "1985-06-19", "1985-06-19", "1985-08-01", "1985-08-01", "1990-06-19", "1990-06-19", "1990-06-19", "1990-06-19", "2000-05-12"), spp = c("a", "b", "c", "c", "d", "b", "c", "d", "a", "b"), y = rpois(10, 5)) id date spp y 1 1 1985-06-19 a 6 2 1 1985-06-19 b 3 3 1 1985-06-19 c 7 4 2 1985-08-01 c 7 5 2 1985-08-01 d 6 6 3 1990-06-19 b 5 7 3 1990-06-19 c 4 8 3 1990-06-19 d 4 9 3 1990-06-19 a 6 10 4 2000

Reshape R data with user entries in rows, collapsing for each user

筅森魡賤 提交于 2020-01-19 14:12:08
问题 Pardon my new-ness to the R world, thank you kindly in advance for your help. I would like to analyze the data from an experiment. The data comes in in Long format, and it needs to be reshaped into wide, but I cannot figure out exactly how to do it. Most of the examples for melt/cast and reshape deal with much simpler dataframes. Each time the subject answers a question on the experiment, his userid, location, age, and gender are recorded in a single row, then his experimental data on a

Reshaping 2 column data.table from long to wide

送分小仙女□ 提交于 2020-01-16 19:37:09
问题 This is my data.frame: library(data.table) df<- fread(' predictions Label 3 A 4 B 5 C 1 A 2 B 3 C ') Desired Output: A B C 3 4 5 1 2 3 I am trying DesiredOutput<-dcast(df, Label+predictions ~ Label, value.var = "predictions") with no success. Your help is appreciated! 回答1: Maybe the base R function unstack is the cleanest solution: unstack(df) A B C 1 3 4 5 2 1 2 3 Note that this returns a data.frame rather than a data.table, so if you want a data.table at the end: df2 <- setDT(unstack(df))

wide to long multiple columns issue

佐手、 提交于 2020-01-16 09:48:16
问题 I have something like this: id role1 Approved by Role1 role2 Approved by Role2 1 Amy 1/1/2019 David 4/4/2019 2 Bob 2/2/2019 Sara 5/5/2019 3 Adam 3/3/2019 Rachel 6/6/2019 I want something like this: id Name Role Approved 1 Amy role1 1/1/2019 2 Bob role1 2/2/2019 3 Adam role1 3/3/2019 1 David role2 4/4/2019 2 Sara role2 5/5/2019 3 Rachel role2 6/6/2019 I thought something like this would work melt(df,id.vars= id, measure.vars= list(c("role1", "role2"),c("Approved by Role1", "Approved by Role2")

expand year values to month in pandas

自作多情 提交于 2020-01-16 00:46:14
问题 I have sales by year: pd.DataFrame({'year':[2015,2016,2017],'value':['12','24','30']}) year value 0 2015 12 1 2016 24 2 2017 36 I want to extrapolate to months: yyyymm value 201501 1 (ie 12/12, etc) 201502 1 ... 201512 1 201601 2 ... 201712 3 any suggestions? 回答1: One idea is use cross join with helper DataFrame, convert columns to strings and add 0 by Series.str.zfill: df1 = pd.DataFrame({'m': range(1, 13), 'a' : 1}) df = df.assign(a = 1).merge(df1).drop('a', 1) df['year'] = df['year']

How to reshape data for a stacked barchart using R lattice [duplicate]

只谈情不闲聊 提交于 2020-01-15 11:15:12
问题 This question already has answers here : Reshaping data.frame from wide to long format (7 answers) Closed 5 years ago . I have a bunch of data in a table (imported from csv) in the following format: date classes score 9/1/11 french 34 9/1/11 english 34 9/1/11 french 34 9/1/11 spanish 34 9/2/11 french 34 9/2/11 english 34 9/3/11 spanish 34 9/3/11 spanish 34 9/5/11 spanish 34 9/5/11 english 34 9/5/11 french 34 9/5/11 english 34 Ignore the score column, it's not important. I need a tally of the

How to reshape a tensor with multiple `None` dimensions?

醉酒当歌 提交于 2020-01-13 11:13:59
问题 I encountered a problem to reshape an intermediate 4D tensorflow tensor X to a 3D tensor Y , where X is of shape ( batch_size, nb_rows, nb_cols, nb_filters ) Y is of shape ( batch_size, nb_rows*nb_cols, nb_filters ) batch_size = None Of course, when nb_rows and nb_cols are known integers, I can reshape X without any problem. However, in my application I need to deal with the case nb_rows = nb_cols = None What should I do? I tried Y = tf.reshape( X, (-1, -1, nb_filters)) but it clearly fails

How to reshape a tensor with multiple `None` dimensions?

做~自己de王妃 提交于 2020-01-13 11:11:12
问题 I encountered a problem to reshape an intermediate 4D tensorflow tensor X to a 3D tensor Y , where X is of shape ( batch_size, nb_rows, nb_cols, nb_filters ) Y is of shape ( batch_size, nb_rows*nb_cols, nb_filters ) batch_size = None Of course, when nb_rows and nb_cols are known integers, I can reshape X without any problem. However, in my application I need to deal with the case nb_rows = nb_cols = None What should I do? I tried Y = tf.reshape( X, (-1, -1, nb_filters)) but it clearly fails

Reshape Panel Data Wide Format to Long Format

こ雲淡風輕ζ 提交于 2020-01-13 10:08:59
问题 I am struggling with transformation of a Panel Dataset from wide to long format. The Dataset looks like this: ID | KP1_430a | KP1_430b | KP1_430c | KP2_430a | KP2_430b | KP2_430c | KP1_1500a | ... 1 .... 2 .... KP1; KP2 up to KP7 describe the Waves. a,b up to f describe a specific Item. (E.g. left to right right placement of Party a) I would like to have this data in long format. Like this: ID | Party | Wave | 430 | 1500 1 1 1 .. .. 1 2 1 .. .. . . . 1 1 2 .. .. . . . 2 1 1 .. .. I tried to