logo
down
shadow

Select values based on other columns


Select values based on other columns

By : user2172926
Date : October 22 2020, 08:10 AM
I hope this helps . 1) Here Mean does the calculation for one row and we apply it to each row separately. We are assuming here you want to zero elements in the first 3 columns whose corresponding column among the last 3 columns are positive and then take the mean of that.
code :
Mean <- function(x) mean(x[1:3] * (x[4:6] > 0))
transform(df2, desired = apply(df2, 1, Mean))
    a1   b1   c1    a2    b2    c2   desired
1 0.51 0.49 0.48  0.05  0.03  0.09 0.4933333
2 0.33 0.31 0.30 -0.03 -0.05  0.01 0.1000000
3 0.22 0.20 0.19  0.04  0.02  0.08 0.2033333
4 0.54 0.52 0.51 -0.05  0.08 -0.01 0.1733333
5 0.45 0.43 0.42 -0.03 -0.05  0.01 0.1400000
transform(df2, desired = rowMeans(df2[1:3] * (df2[4:6] > 0)))
    a1   b1   c1    a2    b2    c2   desired
1 0.51 0.49 0.48  0.05  0.03  0.09 0.4933333
2 0.33 0.31 0.30 -0.03 -0.05  0.01 0.1000000
3 0.22 0.20 0.19  0.04  0.02  0.08 0.2033333
4 0.54 0.52 0.51 -0.05  0.08 -0.01 0.1733333
5 0.45 0.43 0.42 -0.03 -0.05  0.01 0.1400000
Lines <- "
a1       b1      c1      a2      b2      c2 
0.51    0.49    0.48    0.05    0.03    0.09
0.33    0.31    0.3    -0.03    -0.05   0.01
0.22    0.2     0.19    0.04    0.02    0.08
0.54    0.52    0.51    -0.05   0.08    -0.01
0.45    0.43    0.42    -0.03   -0.05   0.01"
df2 <- read.table(text = Lines, header = TRUE)


Share : facebook icon twitter icon
R - Select Columns based on the values on each respective columns

R - Select Columns based on the values on each respective columns


By : selva rani
Date : March 29 2020, 07:55 AM
hop of those help? If I have a dataframe with 5 columns (A to E) with 1000 rows, all columns have variety of numbers greater than 0 except 2 columns (D and E) which has only zeros. How do I evaluate each columns so that i select only columns with values? I want to have a new dataframe with columns (A B C). In my actual dataframe, i have a thousand columns. All I know is the dplyr select(dataframe, ) , You can remove columns which are all 0 or NA with:
code :
x <- x[,colSums(x,na.rm = TRUE) > 0]
Select columns based on values from any table(number of columns is variable) SQL

Select columns based on values from any table(number of columns is variable) SQL


By : just visual basic
Date : March 29 2020, 07:55 AM
around this issue I have a following table: , Wish this will have some help.
code :
SET NOCOUNT  ON 

DECLARE 
    @tablename VARCHAR(50) = 'Table1',
    @valuetocompare INT = 50,
    @otherfields VARCHAR(100) = 'Date, Hour,';

DECLARE @t AS TABLE (cname VARCHAR(10), cvalue INT)
DECLARE @sql NVARCHAR(1000);
DECLARE @cname VARCHAR(128);
DECLARE c CURSOR  
FOR
    SELECT NAME
    FROM   sys.[columns] AS c
    WHERE  c.[object_id] = OBJECT_ID(@tablename)
;

OPEN c;
FETCH NEXT  FROM c INTO @cname;

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql =  'select ''' + @cname  + ''', ' + @cname + ' from '  + @tablename;

    INSERT INTO @t
      (
        cname,
        cvalue
      )
    EXECUTE (@sql);

    FETCH NEXT FROM c  INTO @cname;
END

CLOSE c;
DEALLOCATE c;

DECLARE @cnames VARCHAR(100) = '';
WITH dcnames AS (
         SELECT DISTINCT cname
         FROM   @t
         WHERE  cvalue < @valuetocompare
     )
SELECT @cnames = @cnames + cname +  ','
FROM   dcnames;

IF @cnames =  ''
    PRINT 'No column value is less than ' + CAST(@valuetocompare AS VARCHAR);
ELSE
BEGIN
      SET @sql =  'select ' + @otherfields  + LEFT(@cnames, LEN(@cnames) - 1)  + ' from ' + @tablename;
      EXECUTE (@sql);
END
How to select rows in pandas dataframe based on values in other columns and How to select one row for each distinct valu

How to select rows in pandas dataframe based on values in other columns and How to select one row for each distinct valu


By : Trina Wello
Date : March 29 2020, 07:55 AM
I hope this helps you . For first solution is easier filtered twice - first only Operational and then duplicates:
code :
df1 = df[df['C3'] == 'Operational'] 
df1 = df1[df1.duplicated(['C2'], keep=False)]
print (df1)
     C1    C2           C3   C4
0  1234  1002  Operational  ABC
2  7896  1002  Operational  DEF
m1 = df['C3'] == 'Operational'
df1 = df[df[m1].duplicated(['C2'], keep=False) & m1]
print (df1)
     C1    C2           C3   C4
0  1234  1002  Operational  ABC
2  7896  1002  Operational  DEF
df2 = df.drop_duplicates('C4', keep='last')
print (df2)
     C1    C2           C3   C4
2  7896  1002  Operational  DEF
3  4321  4005       Closed  CDE
4  7781  4005  Operational  ABC
mask = df['C4'].ne(df['C4'].shift()).cumsum().duplicated(keep=False)
df2 = df[~mask]
print (df2)
     C1    C2           C3   C4
2  7896  1002  Operational  DEF
3  4321  4005       Closed  CDE
4  7781  4005  Operational  ABC
Python scatterplot design - select specific values of a variable for the x axis based on another columns values

Python scatterplot design - select specific values of a variable for the x axis based on another columns values


By : Ali Osman Yılmaz
Date : March 29 2020, 07:55 AM
To fix the issue you can do There are most likely multiple ways to solve your problem. The method I'd take is to first transform you dataset in such a way that there is a single row (observation) for each participant, and where (for each row) there is one column that reports the means where MissingLimb is 0 and another column that reports the means where MissingLimb is 1.
You can accomplish this data transformation with this code:
code :
df = pd.pivot_table(ErrorMedianScatter, 
                    values='mean', 
                    index='participant', 
                    columns='MissingLimb')

df.columns = ['MissingLimb 0', 'MissingLimb 1']
sns.lmplot(data=df, x='MissingLimb 0', y='MissingLimb 1')
select columns' values based on another two values from another column

select columns' values based on another two values from another column


By : Dejan Ćosić
Date : March 29 2020, 07:55 AM
Any of those help I have this data set and I need to extract multiple columns values based on on column value. , Are you looking for customers of both stores? One method uses exists:
code :
with t as (
      SELECT i.inventory_id, i.store_id,
             r.customer_id, c.first_name, c.last_name
      FROM inventory i JOIN
           rental r
           ON i.inventory_id = r.inventory_id JOIN
           customer c
           ON r.customer_id = c.customer_id
      WHERE i.film_id = 25
     )
select t.*
from t
where t.store_id in ( 1, 2) and
      exists (select 1
              from t t2
              where t2.customer_id = t.customer_id and
                    t2.store_id in (1, 2) and
                    t2.store_id <> t.store_id
             );
SELECT r.customer_id, c.first_name, c.last_name
FROM inventory i JOIN
     rental r
     ON i.inventory_id = r.inventory_id JOIN
     customer c
     ON r.customer_id = c.customer_id
WHERE i.film_id = 25 AND
      r.store_id in (1, 2)
GROUP BY r.customer_id, c.first_name, c.last_name
HAVING COUNT(DISTINCT r.store_id) = 2;
Related Posts Related Posts :
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • ROC curve from train/test set in caret R package
  • Random Forest for a mixture of categorical,numeric and "unwanted" variables which include missing values
  • extract certain data from multiple excel files with R
  • Matrix with counts of wins and losses between methods in R
  • Grouping string variables from a dataframe by best string match to make subsets
  • Reorder does not work after adding second geom_points
  • cover POS data formate to the one can apply Arules (Apriori)
  • Matching values between data frames based on overlapping dates
  • Grouped bar chart turns into stacked bar chart ggplot
  • R: How to fill in NA Values within a Column based on grouping?
  • Two action buttons, but only the first one, that is written in the server file, works?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org