data-manipulation

Create a variable capturing the most frequent occurence by group

人走茶凉 提交于 2019-11-27 03:32:08
问题 Define: df1 <-data.frame( id=c(rep(1,3),rep(2,3)), v1=as.character(c("a","b","b",rep("c",3))) ) s.t. > df1 id v1 1 1 a 2 1 b 3 1 b 4 2 c 5 2 c 6 2 c I want to create a third variable freq that contains the most frequent observation in v1 by id s.t. > df2 id v1 freq 1 1 a b 2 1 b b 3 1 b b 4 2 c c 5 2 c c 6 2 c c 回答1: You can do this using ddply and a custom function to pick out the most frequent value: myFun <- function(x){ tbl <- table(x$v1) x$freq <- rep(names(tbl)[which.max(tbl)],nrow(x))

Subsetting a matrix by row.names

走远了吗. 提交于 2019-11-27 01:22:52
问题 I have a matrix with the following row.names: "X1" "X5" "X33" "X37" "X52" "X566" Now I want to select only the rows which match the entries of a list, say: include_list <- c("X1", "X5", "X33") I imagine I'd do something like this: data.subset <- subset(data, row.names == include_list) However, this particular code does not seem to do the job. How can I perform subsetting in this way? 回答1: Set up some fake data: m <- matrix(1:30, 6, 5) rownames(m) <- c("X1", "X5", "X33", "X37", "X52", "X566")

Read a CSV from github into R

自作多情 提交于 2019-11-26 22:10:17
I am trying to read a CSV from github into R: latent.growth.data <- read.csv("https://github.com/aronlindberg/latent_growth_classes/blob/master/LGC_data.csv") However, this gives me: Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : unsupported URL scheme I tried ?read.csv , ?download.file , getURL (which only returned strange HTML), as well as the data import manual , but still cannot understand how to make it work. What am I doing wrong? Try this: library(RCurl) x <- getURL("https://raw.github.com/aronlindberg/latent_growth_classes

Flatten a column with value of type list while duplicating the other column's value accordingly in Pandas

与世无争的帅哥 提交于 2019-11-26 20:44:46
问题 Dear power Pandas experts: I'm trying to implement a function to flatten a column of a dataframe which has element of type list, I want for each row of the dataframe where the column has element of type list, all columns but the designated column to be flattened will be duplicated, while the designated column will have one of the value in the list. The following illustrate my requirements: input = DataFrame({'A': [1, 2], 'B': [['a', 'b'], 'c']}) A B 0 1 [a, b] 1 2 c expected = DataFrame({'A':

Assign value to group based on condition in column

耗尽温柔 提交于 2019-11-26 16:44:51
问题 I have a data frame that looks like the following: > df = data.frame(group = c(1,1,1,2,2,2,3,3,3), date = c(1,2,3,4,5,6,7,8,9), value = c(3,4,3,4,5,6,6,4,9)) > df group date value 1 1 1 3 2 1 2 4 3 1 3 3 4 2 4 4 5 2 5 5 6 2 6 6 7 3 7 6 8 3 8 4 9 3 9 9 I want to create a new column that contains the date value per group that is associated with the value "4" from the value column. The following data frame shows what I hope to accomplish. group date value newValue 1 1 1 3 2 2 1 2 4 2 3 1 3 3 2 4

jQuery jqGrid Show message when an edit row is complete

天涯浪子 提交于 2019-11-26 16:44:14
I am following this tutorial here http://www.trirand.com/blog/jqgrid/jqgrid.html in LiveDataManipulation->EditRow My grid receive data from script a.php . After the user can modify this data by the jqGrid. jqGrid after the modification data will send data to script B.php that update my database and return a message of response like "all goes well". I want that this response is alerted or showed to user somewhere on the page. Reading the tutorial and here http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing I think that I've to use afterSubmit option, but I haven't understood how

Converting String Array to an Integer Array

做~自己de王妃 提交于 2019-11-26 14:23:21
问题 so basically user enters a sequence from an scanner input. 12, 3, 4 , etc. It can be of any length long and it has to be integers. I want to convert the string input to an integer array. so int[0] would be 12 , int[1] would be 3 , etc. Any tips and ideas? I was thinking of implementing if charat(i) == ',' get the previous number(s) and parse them together and apply it to the current available slot in the array. But I'm not quite sure how to code that. 回答1: You could read the entire input line

Read a CSV from github into R

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 08:09:20
问题 I am trying to read a CSV from github into R: latent.growth.data <- read.csv(\"https://github.com/aronlindberg/latent_growth_classes/blob/master/LGC_data.csv\") However, this gives me: Error in file(file, \"rt\") : cannot open the connection In addition: Warning message: In file(file, \"rt\") : unsupported URL scheme I tried ?read.csv , ?download.file , getURL (which only returned strange HTML), as well as the data import manual, but still cannot understand how to make it work. What am I

jQuery jqGrid Show message when an edit row is complete

二次信任 提交于 2019-11-26 04:55:53
问题 I am following this tutorial here http://www.trirand.com/blog/jqgrid/jqgrid.html in LiveDataManipulation->EditRow My grid receive data from script a.php . After the user can modify this data by the jqGrid. jqGrid after the modification data will send data to script B.php that update my database and return a message of response like \"all goes well\". I want that this response is alerted or showed to user somewhere on the page. Reading the tutorial and here http://www.trirand.com/jqgridwiki

How can I access and process nested objects, arrays or JSON?

 ̄綄美尐妖づ 提交于 2019-11-25 22:51:13
问题 I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)? For example: var data = { code: 42, items: [{ id: 1, name: \'foo\' }, { id: 2, name: \'bar\' }] }; How could I access the name of the second item in items ? 回答1: Preliminaries JavaScript has only one data type which can contain multiple values: Object . An Array is a special form of object. (Plain) Objects have the form {key: value, key: value,