apply

R: Apply, custom function and Dataframe names

我与影子孤独终老i 提交于 2019-12-12 03:53:58
问题 I have a data frame Indices containing various names which correspond to data frames ( i.e. a name "Index 1" has a corresponding data frame Index 1 ). Now I want to run my custom function calcScores over all data frames and add several columns to that data frame. Since I'm not in the global environment I return that "new" data frame and want to assign it back to the original variable Index 1 , so Index 1 now has the new data frame with the added columns. Here's my code (no chance I can make

boost::variant - get vector property of members

眉间皱痕 提交于 2019-12-12 03:39:08
问题 I am stuck :). I have two-level hierarchy, each level has child nodes. The goal is to use this structure to populate gui tree. I am trying to access child nodes of variant members in a generic manner. Following code does not compile, using vs2013. I'll appreciate the helping hand and/or advise on the design changes. #include "stdafx.h" #include <memory> #include <string> #include <vector> #include <boost/variant.hpp> class base {}; class A : public base { public: std::vector<std::shared_ptr

How can I for each row calculate the sum and write the percentage of that sum near each value

耗尽温柔 提交于 2019-12-12 00:46:49
问题 I have a table: X1 X2 X3 Y1 10 12 15 Y2 45 5 23 Y3 12 47 56 How can I for each row calculate the sum and write the percentage of that sum near each value. For example: X1 percent X2 percent X3 percent Y1 10 27% 12 32% 15 40.5% Y2 45 .. 5 .. 23.. Y3 12 .. 47 .. 56.. 回答1: You could either use. If dat is the dataset ( dat is data.frame in this example) v1 <- paste0(round(prop.table(as.matrix(dat),margin=1),2)*100,"%") #suggested by @Ananda Mahto or v2 <- matrix(paste0(100*round(as.matrix(dat)

Optimization of an R loop taking 18 hours to run

人走茶凉 提交于 2019-12-12 00:15:20
问题 I've got an R code that works and does what I want but It takes a huge time to run. Here is an explanation of what the code does and the code itself. I've got a vector of 200000 line containing street adresses (String) : data. Example : > data[150000,] address "15 rue andre lalande residence marguerite yourcenar 91000 evry france" And I have a matrix of 131x2 string elements which are 5grams (part of word) and the ids of the bags of NGrams (example of a 5Grams bag : ["stack", "tacko", "ackov"

rename filename with a data column

假装没事ソ 提交于 2019-12-11 20:19:19
问题 I hope this question is clear and feasible. I have many csv files labelled with a simple numeric ID (e.g., 1.csv, 2.csv, 3.csv, etc.). Another csv file contains the actual data like below ID Site Location Block 1 L1 1 a 2 L2 2 b 3 etc My question is how to link the ID in the data file to rename the csv files. The output I would ideally like to have is L11a.csv, L22b.csv, etc. I think I need to concatenate each row observation's Site, Location, and Block into another column and then use that

If statement in user defined function within apply in R

只谈情不闲聊 提交于 2019-12-11 17:51:18
问题 Forgive me if this is a blatantly obvious question, I am a beginner R user eager to learn. I have a data frame of 4 columns with roughly 1.5 million rows containing coordinate information where each individual row represents a specific location. What I would like to do is run these data into a function that holds a series of if else statements that determine the area of the specific location within a larger box. For example, a point can be in the center, along the edge of the box within 1.5

creating new column based on whether the letter 'l' or 'L' is in the string of another column

痴心易碎 提交于 2019-12-11 16:12:26
问题 I am working with the Open Food Facts dataset which is very messy. There is a column called quantity in which in information about the quantity of respective food. the entries look like: 365 g (314 ml) 992 g 2.46 kg 0,33 litre 15.87oz 250 ml 1 L 33 cl ... and so on (very messy!!!) I want to create a new column called is_liquid . My idea is that if the quantity string contains an l or L the is_liquid field in this row should get a 1 and if not 0. Here is what I've tried: I wrote this function:

Alter number string in pandas column

风格不统一 提交于 2019-12-11 15:32:56
问题 Background I have a sample df with a Text column containing 0,1, or >1 ABC 's import pandas as pd df = pd.DataFrame({'Text' : ['Jon J Mmith ABC: 1111111 is this here', 'ABC: 1234567 Mary Lisa Rider found here', 'Jane A Doe is also here', 'ABC: 2222222 Tom T Tucker is here ABC: 2222222 too'], 'P_ID': [1,2,3,4], 'N_ID' : ['A1', 'A2', 'A3', 'A4'] }) #rearrange columns df = df[['Text','N_ID', 'P_ID']] df Text N_ID P_ID 0 Jon J Mmith ABC: 1111111 is this here A1 1 1 ABC: 1234567 Mary Lisa Rider

R sapply vs apply vs lapply + as.data.frame

被刻印的时光 ゝ 提交于 2019-12-11 14:15:20
问题 I'm working with some Date columns and trying to cleanse for obviously incorrect dates. I've written a function using the safe.ifelse function mentioned here. Here's my toy data set: df1 <- data.frame(id = 1:25 , month1 = seq(as.Date('2012-01-01'), as.Date('2014-01-01'), by = 'month' ) , month2 = seq(as.Date('2012-01-01'), as.Date('2014-01-01'), by = 'month' ) , month3 = seq(as.Date('2012-01-01'), as.Date('2014-01-01'), by = 'month' ) , letter1 = letters[1:25] ) This works fine for a single

Clojure Passing Individual Variables to Function

元气小坏坏 提交于 2019-12-11 13:43:27
问题 I am trying to pass some vectors containing a list of nodes in to a function in clojure the function works if i was to type the variables in but i am not sure how to pass a single variable from each vector in at a time. (def ItemPickUp [:a1 :Mail]) (def ItemDestinations [:Storage :a1]) (def Robot {[ItemPickUp] [ItemDestinations]}) (defn shortestPath [g start dest] (let [not-destination? (fn [[vertex _]] (not= vertex dest))] (-> (shortest-paths g start) (->> (drop-while not-destination?))