  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Select values based on other columns  » r » 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"
`````` ## 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

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

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)
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

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

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;
`````` 