f******t 发帖数: 18 | 1 这样?O(n),n是matrix元素数目
void dfs(hash_set&s, vector>& matrix,int row,int col)
{
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
s.insert(row*matrix[0].size()+col);
for(int i=0;i<4;i++)
{
int newRow = row+dir[i][0],newCol = col+dir[i][1];
if(newRow>=0&&new
newCol>=0&&newCol
matrix[newRow][newCol]!=-1)
dfs(s,matrix,newRow,newCol);
}
}
int regionNum(vector>& matrix)
{... 阅读全帖 |
|
y*****i 发帖数: 3 | 2 Sorry I can't input Chinese with this computer...
and thank you for taking a look,
and here is my challenge:
I want to add new a numeric column 'newcol' to tb1,
and the value of newcol will be decided by item:
if item is 'A' then newcol take valA;
if item is 'B' then newcol take valB;
...
tb1 has 32 columns:
time item valA valB valC valD valE valF....valAD
t1 A 0.1 0.2 . . 0.4
t1 B . . 0.1 0.2 . . 0.4
t1 c . 0.2 0.3 . . . 0.4
.
.
.
t10 |
|
j*****n 发帖数: 1781 | 3 ***************************************************************
What is wrong with this:
select result=case when type like 'Dec%' then 1 else 0 end.....
what other better way can do the same?
***************************************************************
that's why I suggested that you need ask front-end to do this job...
or, if this query is frequently used, add one more column to handle it:
UPDATE table
SET newCol = 1
FROM table
WHERE type LIKE 'Dec%'
UPDATE table
SET newCol = 0
FROM table
W |
|
f********t 发帖数: 117 | 4 selct *, case when item = A then valA
when item = B then valB
..
end as newcol
from tb1
I didnt test this in sas, but it should work in sql. |
|
A*****O 发帖数: 394 | 5 data <- read.table ("data.txt", header=TRUE)
data
recode <- function(row){
if (row[2]!=row[4]) {x <- as.numeric(row[5])*-1}
else
{x <- as.numeric(row[5])*1}
}
data$newcol <- apply(data,1,recode)
data
------------------------------------
question:
there are some NAs at column 2 and 4, how can I apply "na.rm=true" in the
code above to skip NAs as I analyze the data?
many thanks! |
|
t****a 发帖数: 1212 | 6 1. you can use is.na() function to detect if a value is NA.
2. your code would be very slow when you are computing large data.frame.
because you are using apply or sapply. I would write it with vector
computing like:
data$newcol = ifelse(is.na(data[,2]) | is.na(data[,4]) | data[,2]!=data[,4],
as.numeric(data[,5]) - 1, as.numeric(data[,5])) |
|