R and Pandas: R equivalent of np.sum and np.reshape
up vote
-1
down vote
favorite
I have two line of code which I want to convert to R from Python:
Can anyone convert the below line of code from python to R:
np.sum([[0, 1], [0, 5]], axis=0)
&
np.reshape(li, [-1, N])
li is the list of elements and N is any variable.
python r pandas numpy reshape
add a comment |
up vote
-1
down vote
favorite
I have two line of code which I want to convert to R from Python:
Can anyone convert the below line of code from python to R:
np.sum([[0, 1], [0, 5]], axis=0)
&
np.reshape(li, [-1, N])
li is the list of elements and N is any variable.
python r pandas numpy reshape
You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11
np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have two line of code which I want to convert to R from Python:
Can anyone convert the below line of code from python to R:
np.sum([[0, 1], [0, 5]], axis=0)
&
np.reshape(li, [-1, N])
li is the list of elements and N is any variable.
python r pandas numpy reshape
I have two line of code which I want to convert to R from Python:
Can anyone convert the below line of code from python to R:
np.sum([[0, 1], [0, 5]], axis=0)
&
np.reshape(li, [-1, N])
li is the list of elements and N is any variable.
python r pandas numpy reshape
python r pandas numpy reshape
asked Nov 19 at 13:10
apoorv parmar
346
346
You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11
np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21
add a comment |
You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11
np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21
You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11
You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11
np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21
np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Example data in R -
mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)
This is what it will look like -
[,1] [,2]
[1,] 0 0
[2,] 5 1
1st part
rowSums(mat)
Output will look like this -
[1] 0 6
If this doesn't seem to work as per needs try colSums
(equivalent to varying axis
in numpy
)
2nd Part
matrix(mat, 1, length(mat))
This will flatten the array similar to np.reshape()
[,1] [,2] [,3] [,4]
[1,] 0 5 0 1
This is the part where you want to flatten your matrix. reshape
in numpy
can be re-written in R by calling the matrix()
function to re-cast the existing mat
object
Note
This is a pure R implementation (standard libs). There are many other libraries like rehsape2
that will do it in different ways
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Example data in R -
mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)
This is what it will look like -
[,1] [,2]
[1,] 0 0
[2,] 5 1
1st part
rowSums(mat)
Output will look like this -
[1] 0 6
If this doesn't seem to work as per needs try colSums
(equivalent to varying axis
in numpy
)
2nd Part
matrix(mat, 1, length(mat))
This will flatten the array similar to np.reshape()
[,1] [,2] [,3] [,4]
[1,] 0 5 0 1
This is the part where you want to flatten your matrix. reshape
in numpy
can be re-written in R by calling the matrix()
function to re-cast the existing mat
object
Note
This is a pure R implementation (standard libs). There are many other libraries like rehsape2
that will do it in different ways
add a comment |
up vote
1
down vote
Example data in R -
mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)
This is what it will look like -
[,1] [,2]
[1,] 0 0
[2,] 5 1
1st part
rowSums(mat)
Output will look like this -
[1] 0 6
If this doesn't seem to work as per needs try colSums
(equivalent to varying axis
in numpy
)
2nd Part
matrix(mat, 1, length(mat))
This will flatten the array similar to np.reshape()
[,1] [,2] [,3] [,4]
[1,] 0 5 0 1
This is the part where you want to flatten your matrix. reshape
in numpy
can be re-written in R by calling the matrix()
function to re-cast the existing mat
object
Note
This is a pure R implementation (standard libs). There are many other libraries like rehsape2
that will do it in different ways
add a comment |
up vote
1
down vote
up vote
1
down vote
Example data in R -
mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)
This is what it will look like -
[,1] [,2]
[1,] 0 0
[2,] 5 1
1st part
rowSums(mat)
Output will look like this -
[1] 0 6
If this doesn't seem to work as per needs try colSums
(equivalent to varying axis
in numpy
)
2nd Part
matrix(mat, 1, length(mat))
This will flatten the array similar to np.reshape()
[,1] [,2] [,3] [,4]
[1,] 0 5 0 1
This is the part where you want to flatten your matrix. reshape
in numpy
can be re-written in R by calling the matrix()
function to re-cast the existing mat
object
Note
This is a pure R implementation (standard libs). There are many other libraries like rehsape2
that will do it in different ways
Example data in R -
mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)
This is what it will look like -
[,1] [,2]
[1,] 0 0
[2,] 5 1
1st part
rowSums(mat)
Output will look like this -
[1] 0 6
If this doesn't seem to work as per needs try colSums
(equivalent to varying axis
in numpy
)
2nd Part
matrix(mat, 1, length(mat))
This will flatten the array similar to np.reshape()
[,1] [,2] [,3] [,4]
[1,] 0 5 0 1
This is the part where you want to flatten your matrix. reshape
in numpy
can be re-written in R by calling the matrix()
function to re-cast the existing mat
object
Note
This is a pure R implementation (standard libs). There are many other libraries like rehsape2
that will do it in different ways
answered Nov 19 at 13:28
Vivek Kalyanarangan
4,0521725
4,0521725
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375385%2fr-and-pandas-r-equivalent-of-np-sum-and-np-reshape%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11
np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21