我正在尝试在数据框中创建一个新变量(incomeX) 基于colnames的最后一个字符,如果:
income1 income2 income3 income4 income5
1 0 1 0 0 0
2 1 0 0 0 0
3 1 0 0 0 0
4 1 0 0 0 0
5 1 0 0 0 0
6 1 0 0 0 0
7 0 1 0 0 0
8 1 0 0 0 0
9 1 0 0 0 0
10 0 0 0 1 0
我会得到:
income1 income2 income3 income4 income5 incomeX
1 0 1 0 0 0 2
2 1 0 0 0 0 1
3 1 0 0 0 0 1
4 1 0 0 0 0 1
5 1 0 0 0 0 1
6 1 0 0 0 0 1
7 0 1 0 0 0 2
8 1 0 0 0 0 1
9 1 0 0 0 0 1
10 0 0 0 1 0 4
答案 0 :(得分:1)
您可以使用基础r中的var xv = 0;
var yv = 0;
var itemx = firebase.database().ref().child("item").child("xval");
itemx.on('value', function(xvalue){
xv = xvalue.val();
});
var itemy = firebase.database().ref().child("item").child("yval");
itemy.on('value', function(yvalue){
yv = yvalue.val();
});
var start = graph.grid[aa][bb];
var end = graph.grid[xv][yv];
var result = astar.search(graph, start, end);
来解决此问题:
which
答案 1 :(得分:0)
以下是使用tidyverse
的方法。我们将数据转换为整洁的数据形式,将字符income
与数字分开,选择值== 1的行,然后使用rowId
将结果合并回原始数据。
inputData <- " rowId income1 income2 income3 income4 income5
1 0 1 0 0 0
2 1 0 0 0 0
3 1 0 0 0 0
4 1 0 0 0 0
5 1 0 0 0 0
6 1 0 0 0 0
7 0 1 0 0 0
8 1 0 0 0 0
9 1 0 0 0 0
10 0 0 0 1 0
"
data <- read.table(text=inputData,header=TRUE)
library(dplyr)
library(tidyr)
data %>% gather(variable,value,-rowId) %>%
extract(variable,into = c("varname", "number"),
regex = "([A-Za-z]+)([0-9]+)") %>%
filter(value == 1) %>% rename(incomeX = number) %>%
select(-value,-varname) %>%
left_join(data,.) %>% arrange(rowId)
...和输出:
+ left_join(data,.) %>% arrange(rowId)
Joining, by = "rowId"
rowId income1 income2 income3 income4 income5 incomeX
1 1 0 1 0 0 0 2
2 2 1 0 0 0 0 1
3 3 1 0 0 0 0 1
4 4 1 0 0 0 0 1
5 5 1 0 0 0 0 1
6 6 1 0 0 0 0 1
7 7 0 1 0 0 0 2
8 8 1 0 0 0 0 1
9 9 1 0 0 0 0 1
10 10 0 0 0 1 0 4
>