Skip to main content

Posts

R join a table column from a vector

I would like to get the corresponding values of a vector in a table from a column in another column. (just look below) example: Vector: v = c('A', 'B', 'C') Table : # key Value 'C' 3 'A' 1 'B' 2 When I give the vector v ( A, B, C ) I want to get back the corresponding values in the good order 1, 2, 3 . In reality, the vector is the rownames of a dataset, and I need to replace it with the corresponding values. I was thinking about using the left_join function from Dplyr but I would need 2 tables for this. Thanks for your help by vinalti in StackOverflow on July 05, 2022 . Answer A possible solution in base R : df$Value[match(v, df$key)] #> [1] 1 2 3 Using dplyr : library(dplyr) df %>% mutate(x = Value[match(v, key)]) %>% pull(x) #> [1] 1 2 3 by PaulS on July 05, 2022 . Other helpful answers

Replace values based on ID's for determinate columns

I have this situation: ID n post date el a b c d 100_left 4 50 10/11/2020 y 190 5.41 4 300 100_right 4 50 10/11/2020 n 5.4 5 200 101_left 4 50 10/11/2020 y 180 5.49 6 360 101_right 4 50 10/11/2020 n 5.48 6 180 102_left 4 50 10/11/2020 y 190 5.5 3 300 102_right 4 50 10/11/2020 n 5.46 5 200 103_left 4 50 10/11/2020 y 190 5.39 3 170 103_right 4 50 10/11/2020 n 5.44 3 360 I would like to use the same values of n_left for n_right ( ID ), but just for columns from a to d . Like this: ID n post date el a b c d 100_left 4 50 10/11/2020 y 190 5.41 4 300 100_right 4 50 10/11/2020 n 190 5.41 4 300 101_left 4 50 10/11/2020 y 180 5.49 6 360 101_right 4 50 10/11/2020 n 180 5.49 6 360 102_left 4 50 10/11/2020 y 190 5.5 3 300 102_right 4 50 10/11/2020 n 190 5.5 3 300 103_left 4 50 10/11

SQL: only return results, after a join, where columns with different words represent the same idea (col 1 'dog' col 2 'hound' = match)

SQL flavor is Mode's unique variation, that said SQL flavor doesn't really matter. If you can show me how to do it in one variety of SQL, I'll at least know what to Google to figure it out in this variation. I'm joining two tables and trying to identify columns where the status of an item is the same, however the statuses are written differently between the two tables. Table 1 columns: Name Number Status (available, unavailable, inactive) Table 2 columns: Number Status (unassigned, unavailable, retired) Available = unassigned, unavailable = unavailable, inactive = retired. I am trying to first compare available/unassigned line up, inactive/retired line up, etc. Then I'm trying to return only the results where both status columns do not match, but since they use different words for the same idea I just don't know how to do it. by Ryan Mercer in StackOverflow on July 05, 2022 . Answer I'd simply

For each item, find COUNT of item with lower price

I have a table Items(ID, Name, Price). For every item in Items, I‘d like to receive an output of the form: (ID, Name, Count), where Count is the number of items in Items which are cheaper in price. I have tried running a subquery with the same table but found no solution. It seems so simple, but I‘m a bit stuck. by William Bomb in StackOverflow on July 05, 2022 . Answer SELECT t1.ID, t1.Name, t1.Price, COUNT(t2.id) FROM Items t1 LEFT JOIN Items t2 ON t1.price > t2.price GROUP BY t1.ID, t1.Name, t1.Price I add Price column to the output because you have not told that the presence of the same item name with different prices is not possible. by Akina on July 05, 2022 .

How to create a mysql query that checks if a row exists - if it does I want to insert a new record otherwise increment a value

I have a table which has an ID and an occurrences column. I want to create a single transaction to the db where if ID exists in DB: occurrences++ else: insert new row where occurrences = 1 I saw that it's possible to do an IF THEN statement so I was attempting to do something like this: IF ID = 123 IS NOT NULL THEN UPDATE occurrences = occurrences +1 ELSE INSERT INTO entity_table(123, 1) END IF; by DannyD in StackOverflow on July 05, 2022 . Answer INSERT INTO entity_table SET id=123, occurrences = 1 ON DUPLICATE KEY UPDATE occurrences = occurrences + 1 by Bill Karwin on July 05, 2022 .

SwiftUI animation: move up/down on press, spring on release - how to do the "move up" part

How can I get the blue circles to first move away from the green circle before getting back to it? The animation should be: press and hold: the green circle scales down the blue circles, while scaling down as well, first move "up" (away from their resting position, as if pushed away by the pressure applied) and then down (to touch the green circle again, as if they were pulled back by some gravitational force) release everything springs back into place (bonus) ideally, the blue circles are ejected as the green circle springs up, and they fall back down in their resting position (next to the surface) I got everything working except the blue circles moving up part. This is the current animation: And its playground. import SwiftUI import PlaygroundSupport struct DotsView: View { var diameter: CGFloat = 200 var size: CGFloat = 25 var isPressed: Bool = false var body: some View { ZStack { ForEach(0...5, id: \.self) { i in

How to make button visible in scrollview if it's related textfield has been focused

I have a ScrollView which contains VStack which contains a large amount of entries. The amount is not important it is just that you have to scroll to get to a TextField at the end of the list. Below the text field is a "submit"- Button . If the user taps on the text field, the scrollview will adjust its scrolling position to make the textfield visible. But I need, that the button below the input becomes visible, too. Does someone have any idea how I could achieve this behavior? Thanks! Abstract code ScrollView { VStack { // ... content // Button aera VStack { Text("Text") Button("Action Text") { onSubmitRequested() } } } } by Tobonaut in StackOverflow on July 05, 2022 . Answer Here is a possible solution - based on scrollTo , focused state, and animation alignment, latter is important, becaus