有没有一种方法可以按如下方式划分两个数据帧并获得所需的输出? >z 1月20日2月21日3月21日4月21日5月21日6月21日 1 8026 4464 3806 3799 2466 2388 2 0 4743 4877 4769 2008 1503 3 0 0 4516 4474 1532 1160 4 0 0 0 4867 1078 1384 5 0 0 0 0 1274 1439 6 0 0 0 0 0 1359 >x 1月20日2月21日3月21日4月21日5月21日6月21日 1 6093 2949 781 571 1202 1359 2 6093 2949 781 571 1202 1359 3 6093 2949 781 571 1202 1359 4 6093 2949 781 571 1202 1359 5 6093 2949 781 571 1202 1359 6 6093 2949 781 571 1202 1359 >x/z 1月20日2月21日3月21日4月21日5月21日6月21日 1 0.7591577 0.6606183 0.2052023 0.1503027 0.4874290 0.5690955 2 Inf 0.6217584 0.1601394 0.1197316 0.5986056 0.9041916 3 Inf Inf 0.1729407 0.1276263 0.7845953 1.1715517 4 Inf 0.1173207 1.1150278 0.9819364 5 Inf 0.9434851 0.9444058 6 Inf 1.0000000 w%mutate_all(函数(x)ifelse(是无限的(x),0,x)) >w 1月20日2月21日3月21日4月21日5月21日6月21日 1 0.7591577 0.6606183 0.2052023 0.1503027 0.4874290 0.5690955 2 0.0000000 0.6217584 0.1601394 0.1197316 0.5986056 0.9041916 3 0.0000000 0.0000000 0.1729407 0.1276263 0.7845953 1.1715517 4 0.0000000 0.0000000 0.0000000 0.1173207 1.1150278 0.9819364 5 0.0000000 0.0000000 0.0000000 0.0000000 0.9434851 0.9444058 6 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 现在我希望输出如下 Jan20 0.7591577 0.6217584 0.1729407 0.1173207 0.9434851 1.0000000 2月21日0.6606183 0.1601394 0.1276263 1.1150278 0.9444058 0 Mar21 0.2055223 0.1197316 0.7845953 0.9819364 0 0 四月21日0.1503027 0.5986056 1.1715517 0 0
像上面一样,我正在寻找输出。让我知道我们有什么选择 谢谢有没有一种方法可以按如下方式划分两个数据帧并获得所需的输出? >z 1月20日2月21日3月21日4月21日5月21日6月21日 1 8026 4464 3806 3799 2466 2388 2 0 4743 4877 4769 2008 1503 3 0 0 4516 4474 1532 1160 4 0 0 0 4867 1078 1384 5 0 0 0 0 1274 1439 6 0 0 0 0 0 1359 >x 1月20日2月21日3月21日4月21日5月21日6月21日 1 6093 2949 781 571 1202 1359 2 6093 2949 781 571 1202 1359 3 6093 2949 781 571 1202 1359 4 6093 2949 781 571 1202 1359 5 6093 2949 781 571 1202 1359 6 6093 2949 781 571 1202 1359 >x/z 1月20日2月21日3月21日4月21日5月21日6月21日 1 0.7591577 0.6606183 0.2052023 0.1503027 0.4874290 0.5690955 2 Inf 0.6217584 0.1601394 0.1197316 0.5986056 0.9041916 3 Inf Inf 0.1729407 0.1276263 0.7845953 1.1715517 4 Inf 0.1173207 1.1150278 0.9819364 5 Inf 0.9434851 0.9444058 6 Inf 1.0000000 w%mutate_all(函数(x)ifelse(是无限的(x),0,x)) >w 1月20日2月21日3月21日4月21日5月21日6月21日 1 0.7591577 0.6606183 0.2052023 0.1503027 0.4874290 0.5690955 2 0.0000000 0.6217584 0.1601394 0.1197316 0.5986056 0.9041916 3 0.0000000 0.0000000 0.1729407 0.1276263 0.7845953 1.1715517 4 0.0000000 0.0000000 0.0000000 0.1173207 1.1150278 0.9819364 5 0.0000000 0.0000000 0.0000000 0.0000000 0.9434851 0.9444058 6 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 现在我希望输出如下 Jan20 0.7591577 0.6217584 0.1729407 0.1173207 0.9434851 1.0000000 2月21日0.6606183 0.1601394 0.1276263 1.1150278 0.9444058 0 Mar21 0.2055223 0.1197316 0.7845953 0.9819364 0 0 四月21日0.1503027 0.5986056 1.1715517 0 0,r,R,像上面一样,我正在寻找输出。让我知道我们有什么选择 谢谢 Rohit我们可以使用apply循环行,删除0并返回列表 > z Jan20 Feb21 Mar21 Apr21 May21 Jun21 1 8026 4464 3806 3799 2466 2388 2 0 4743 4877 4769 2008 1503 3 0 0 4516 4474 1532 1160 4 0 0 0 4867 1078
Rohit我们可以使用
apply
循环行,删除0并返回列表
> z
Jan20 Feb21 Mar21 Apr21 May21 Jun21
1 8026 4464 3806 3799 2466 2388
2 0 4743 4877 4769 2008 1503
3 0 0 4516 4474 1532 1160
4 0 0 0 4867 1078 1384
5 0 0 0 0 1274 1439
6 0 0 0 0 0 1359
> x
Jan20 Feb21 Mar21 Apr21 May21 Jun21
1 6093 2949 781 571 1202 1359
2 6093 2949 781 571 1202 1359
3 6093 2949 781 571 1202 1359
4 6093 2949 781 571 1202 1359
5 6093 2949 781 571 1202 1359
6 6093 2949 781 571 1202 1359
> x/z
Jan20 Feb21 Mar21 Apr21 May21 Jun21
1 0.7591577 0.6606183 0.2052023 0.1503027 0.4874290 0.5690955
2 Inf 0.6217584 0.1601394 0.1197316 0.5986056 0.9041916
3 Inf Inf 0.1729407 0.1276263 0.7845953 1.1715517
4 Inf Inf Inf 0.1173207 1.1150278 0.9819364
5 Inf Inf Inf Inf 0.9434851 0.9444058
6 Inf Inf Inf Inf Inf 1.0000000
w <- w %>% mutate_all(function(x) ifelse(is.infinite(x), 0, x))
> w
Jan20 Feb21 Mar21 Apr21 May21 Jun21
1 0.7591577 0.6606183 0.2052023 0.1503027 0.4874290 0.5690955
2 0.0000000 0.6217584 0.1601394 0.1197316 0.5986056 0.9041916
3 0.0000000 0.0000000 0.1729407 0.1276263 0.7845953 1.1715517
4 0.0000000 0.0000000 0.0000000 0.1173207 1.1150278 0.9819364
5 0.0000000 0.0000000 0.0000000 0.0000000 0.9434851 0.9444058
6 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
Now I want the output as follows
Jan20 0.7591577 0.6217584 0.1729407 0.1173207 0.9434851 1.0000000
Feb21 0.6606183 0.1601394 0.1276263 1.1150278 0.9444058 0
Mar21 0.2052023 0.1197316 0.7845953 0.9819364 0 0
Apr21 0.1503027 0.5986056 1.1715517 0 0 0
在base R中,您可以执行以下操作:
w_lst1
#$Jan20
#[1] 0.7591577 0.6217584 0.1729407 0.1173207 0.9434851 1.0000000
#$Feb21
#[1] 0.6606183 0.1601394 0.1276263 1.1150278 0.9444058 0.0000000
#$Mar21
#[1] 0.2052023 0.1197316 0.7845953 0.9819364 0.0000000 0.0000000
#$Apr21
#[1] 0.1503027 0.5986056 1.1715517 0.0000000 0.0000000 0.0000000
#$May21
#[1] 0.4874290 0.9041916 0.0000000 0.0000000 0.0000000 0.0000000
#$Jun21
#[1] 0.5690955 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
w这里有一个替代解决方案:
库(tidyverse)
(w[,1][,2][,3][,4][,5][,6]
#> [1,] 0.7591577 0.6606183 0.2052023 0.1503027 0.4874290 0.5690955
#> [2,] 0.0000000 0.6217584 0.1601394 0.1197316 0.5986056 0.9041916
#> [3,] 0.0000000 0.0000000 0.1729407 0.1276263 0.7845953 1.1715517
#> [4,] 0.0000000 0.0000000 0.0000000 0.1173207 1.1150278 0.9819364
#> [5,] 0.0000000 0.0000000 0.0000000 0.0000000 0.9434851 0.9444058
#> [6,] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
helper#A tible:6 x 6
#>V1 V2 V3 V4 V5 V6
#>
#> 1 0.759 0.622 0.173 0.117 0.943 1
#> 2 0.661 0.160 0.128 1.12 0.944 0
#> 3 0.205 0.120 0.785 0.982 0 0
#> 4 0.150 0.599 1.17 0 0 0
#> 5 0.487 0.904 0 0 0 0
#> 6 0.569 0 0 0 0 0
由(v0.3.0)于2020-11-23创建,谢谢Onyanbu。有可能从输出中删除“Jan20,Feb21…Jun21”吗?这就是我所做的,它工作了……“谢谢akrun。我想我们需要“w[Is.infinite(w)]@RohitGosain我假设您在代码中使用了w%的mutate_all(函数(x)ifelse(Is.infinite(x),0,x))
w_lst1
#$Jan20
#[1] 0.7591577 0.6217584 0.1729407 0.1173207 0.9434851 1.0000000
#$Feb21
#[1] 0.6606183 0.1601394 0.1276263 1.1150278 0.9444058 0.0000000
#$Mar21
#[1] 0.2052023 0.1197316 0.7845953 0.9819364 0.0000000 0.0000000
#$Apr21
#[1] 0.1503027 0.5986056 1.1715517 0.0000000 0.0000000 0.0000000
#$May21
#[1] 0.4874290 0.9041916 0.0000000 0.0000000 0.0000000 0.0000000
#$Jun21
#[1] 0.5690955 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
w <- as.matrix(x/z)
w[is.infinite(w)] <- 0
grp <- col(w) - row(w)
grp[grp < 0] <- grp[grp < 0] + ncol(w)
do.call(rbind,setNames(split(t(w), t(grp)), names(x)))
[,1] [,2] [,3] [,4] [,5] [,6]
Jan20 0.7591577 0.6217584 0.1729407 0.1173207 0.9434851 1
Feb21 0.6606183 0.1601394 0.1276263 1.1150278 0.9444058 0
Mar21 0.2052023 0.1197316 0.7845953 0.9819364 0.0000000 0
Apr21 0.1503027 0.5986056 1.1715517 0.0000000 0.0000000 0
May21 0.4874290 0.9041916 0.0000000 0.0000000 0.0000000 0
Jun21 0.5690955 0.0000000 0.0000000 0.0000000 0.0000000 0