adjusting number of rows for each element in a list
up vote
0
down vote
favorite
I have a code that reads certain files and extracts specified data for further processing. The sample of a code is presented below:
library(readxl)
library(openxlsx)
revisedFiles<-choose.files()
dataList<-list()
length(dataList)<-3
## 'For' loop to select only relevant data
for( i in 1:length(revisedFiles)){
#Reading files
dummy<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("F:I"))
dummy2<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("D:D"))
#Attempt to check if number of rows are the same and work around it
if(nrow(dummy)<nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy[nrow(dummy)+diff,]<-c(rep("NA",length(diff)))
} else if(nrow(dummy)>nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy2[nrow(dummy2)+diff,]<-c(rep("NA",length(diff)))
}
# Separating data into 3 categories
nData<-cbind(dummy2,dummy[1])
sData<-cbind(dummy2,dummy[2])
bData<-cbind(dummy2,dummy[3])
#Adding each category to the separate level in the list
dataList[[1]]<-append(dataList[[1]],list(nData))
dataList[[2]]<-append(dataList[[2]],list(sData))
dataList[[3]]<-append(dataList[[3]],list(bData))
}
The problem is that I have the following error:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 231, 232
Previously, everyone suggested to make all elements the same length (by finding the largest element and make all elements of the same size).
Thanks to the Andre in the comments, I've added the 'if' function that seems to bypass the error. However, each level of a list has 7000+ lists when I only expect 26 (because I have 26 files that I read in the first place).
The example would be:
Suppose I have these 3 tables saved in 3 different s/s:
Table 1 Table 2 Table 3
Name1 1 11 111 Name1 4 44 444 Name1 7 77 777
Name2 2 22 222 Name2 5 55 555 Name2 8 88 888
Name3 3 33 333 Name3 6 66 666 Name3 9 99 999
I want to have a list that have 3 levels (1st level for 1 digit numbers,2nd level for 2 digit and 3rd level for 3 digit numbers). In each level, I want to keep values from each table separate. The output for this example would be,
[[1]]
[[1]][[1]]
1,2,3
[[1]][[2]]
4,5,6
[[1]][[3]]
7,8,9
Same for the other levels.
Thanks!
r
|
show 1 more comment
up vote
0
down vote
favorite
I have a code that reads certain files and extracts specified data for further processing. The sample of a code is presented below:
library(readxl)
library(openxlsx)
revisedFiles<-choose.files()
dataList<-list()
length(dataList)<-3
## 'For' loop to select only relevant data
for( i in 1:length(revisedFiles)){
#Reading files
dummy<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("F:I"))
dummy2<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("D:D"))
#Attempt to check if number of rows are the same and work around it
if(nrow(dummy)<nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy[nrow(dummy)+diff,]<-c(rep("NA",length(diff)))
} else if(nrow(dummy)>nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy2[nrow(dummy2)+diff,]<-c(rep("NA",length(diff)))
}
# Separating data into 3 categories
nData<-cbind(dummy2,dummy[1])
sData<-cbind(dummy2,dummy[2])
bData<-cbind(dummy2,dummy[3])
#Adding each category to the separate level in the list
dataList[[1]]<-append(dataList[[1]],list(nData))
dataList[[2]]<-append(dataList[[2]],list(sData))
dataList[[3]]<-append(dataList[[3]],list(bData))
}
The problem is that I have the following error:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 231, 232
Previously, everyone suggested to make all elements the same length (by finding the largest element and make all elements of the same size).
Thanks to the Andre in the comments, I've added the 'if' function that seems to bypass the error. However, each level of a list has 7000+ lists when I only expect 26 (because I have 26 files that I read in the first place).
The example would be:
Suppose I have these 3 tables saved in 3 different s/s:
Table 1 Table 2 Table 3
Name1 1 11 111 Name1 4 44 444 Name1 7 77 777
Name2 2 22 222 Name2 5 55 555 Name2 8 88 888
Name3 3 33 333 Name3 6 66 666 Name3 9 99 999
I want to have a list that have 3 levels (1st level for 1 digit numbers,2nd level for 2 digit and 3rd level for 3 digit numbers). In each level, I want to keep values from each table separate. The output for this example would be,
[[1]]
[[1]][[1]]
1,2,3
[[1]][[2]]
4,5,6
[[1]][[3]]
7,8,9
Same for the other levels.
Thanks!
r
1
well the error seems to be in in the cbind section: =>cbind(mtcars, mtcars[-1,])
. You will need aif
clause to catch and a rule to be applied if the number of rows differ.
– Andre Elrico
Nov 19 at 12:21
So you were right. I've added the 'if' function that I'll edit in and then the 'for' loop worked. However, each level seems to have crazy amount of lists (7000+ when I only expect 26 from 26 files). Would you know why is this a case?
– Num
Nov 19 at 13:34
1
In order to get real and useful help, you will have to provide real data with example and desired outcome. Otherwise its impossible.
– Andre Elrico
Nov 19 at 13:35
@AndreElrico I've added the example and adjusted the code
– Num
Nov 19 at 13:52
1
after#Adding each category to the separate level in the list
try usingdataList[[i]] <- list(nData, sData, dData)
instead of your three lines of code.
– Andre Elrico
Nov 19 at 14:03
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a code that reads certain files and extracts specified data for further processing. The sample of a code is presented below:
library(readxl)
library(openxlsx)
revisedFiles<-choose.files()
dataList<-list()
length(dataList)<-3
## 'For' loop to select only relevant data
for( i in 1:length(revisedFiles)){
#Reading files
dummy<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("F:I"))
dummy2<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("D:D"))
#Attempt to check if number of rows are the same and work around it
if(nrow(dummy)<nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy[nrow(dummy)+diff,]<-c(rep("NA",length(diff)))
} else if(nrow(dummy)>nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy2[nrow(dummy2)+diff,]<-c(rep("NA",length(diff)))
}
# Separating data into 3 categories
nData<-cbind(dummy2,dummy[1])
sData<-cbind(dummy2,dummy[2])
bData<-cbind(dummy2,dummy[3])
#Adding each category to the separate level in the list
dataList[[1]]<-append(dataList[[1]],list(nData))
dataList[[2]]<-append(dataList[[2]],list(sData))
dataList[[3]]<-append(dataList[[3]],list(bData))
}
The problem is that I have the following error:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 231, 232
Previously, everyone suggested to make all elements the same length (by finding the largest element and make all elements of the same size).
Thanks to the Andre in the comments, I've added the 'if' function that seems to bypass the error. However, each level of a list has 7000+ lists when I only expect 26 (because I have 26 files that I read in the first place).
The example would be:
Suppose I have these 3 tables saved in 3 different s/s:
Table 1 Table 2 Table 3
Name1 1 11 111 Name1 4 44 444 Name1 7 77 777
Name2 2 22 222 Name2 5 55 555 Name2 8 88 888
Name3 3 33 333 Name3 6 66 666 Name3 9 99 999
I want to have a list that have 3 levels (1st level for 1 digit numbers,2nd level for 2 digit and 3rd level for 3 digit numbers). In each level, I want to keep values from each table separate. The output for this example would be,
[[1]]
[[1]][[1]]
1,2,3
[[1]][[2]]
4,5,6
[[1]][[3]]
7,8,9
Same for the other levels.
Thanks!
r
I have a code that reads certain files and extracts specified data for further processing. The sample of a code is presented below:
library(readxl)
library(openxlsx)
revisedFiles<-choose.files()
dataList<-list()
length(dataList)<-3
## 'For' loop to select only relevant data
for( i in 1:length(revisedFiles)){
#Reading files
dummy<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("F:I"))
dummy2<-read_excel(revisedFiles[i],sheet="DataSheet",range=cell_cols("D:D"))
#Attempt to check if number of rows are the same and work around it
if(nrow(dummy)<nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy[nrow(dummy)+diff,]<-c(rep("NA",length(diff)))
} else if(nrow(dummy)>nrow(dummy2)){
diff<-abs(nrow(dummy)-nrow(dummy2))
dummy2[nrow(dummy2)+diff,]<-c(rep("NA",length(diff)))
}
# Separating data into 3 categories
nData<-cbind(dummy2,dummy[1])
sData<-cbind(dummy2,dummy[2])
bData<-cbind(dummy2,dummy[3])
#Adding each category to the separate level in the list
dataList[[1]]<-append(dataList[[1]],list(nData))
dataList[[2]]<-append(dataList[[2]],list(sData))
dataList[[3]]<-append(dataList[[3]],list(bData))
}
The problem is that I have the following error:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 231, 232
Previously, everyone suggested to make all elements the same length (by finding the largest element and make all elements of the same size).
Thanks to the Andre in the comments, I've added the 'if' function that seems to bypass the error. However, each level of a list has 7000+ lists when I only expect 26 (because I have 26 files that I read in the first place).
The example would be:
Suppose I have these 3 tables saved in 3 different s/s:
Table 1 Table 2 Table 3
Name1 1 11 111 Name1 4 44 444 Name1 7 77 777
Name2 2 22 222 Name2 5 55 555 Name2 8 88 888
Name3 3 33 333 Name3 6 66 666 Name3 9 99 999
I want to have a list that have 3 levels (1st level for 1 digit numbers,2nd level for 2 digit and 3rd level for 3 digit numbers). In each level, I want to keep values from each table separate. The output for this example would be,
[[1]]
[[1]][[1]]
1,2,3
[[1]][[2]]
4,5,6
[[1]][[3]]
7,8,9
Same for the other levels.
Thanks!
r
r
edited Nov 19 at 13:51
asked Nov 19 at 12:04
Num
4110
4110
1
well the error seems to be in in the cbind section: =>cbind(mtcars, mtcars[-1,])
. You will need aif
clause to catch and a rule to be applied if the number of rows differ.
– Andre Elrico
Nov 19 at 12:21
So you were right. I've added the 'if' function that I'll edit in and then the 'for' loop worked. However, each level seems to have crazy amount of lists (7000+ when I only expect 26 from 26 files). Would you know why is this a case?
– Num
Nov 19 at 13:34
1
In order to get real and useful help, you will have to provide real data with example and desired outcome. Otherwise its impossible.
– Andre Elrico
Nov 19 at 13:35
@AndreElrico I've added the example and adjusted the code
– Num
Nov 19 at 13:52
1
after#Adding each category to the separate level in the list
try usingdataList[[i]] <- list(nData, sData, dData)
instead of your three lines of code.
– Andre Elrico
Nov 19 at 14:03
|
show 1 more comment
1
well the error seems to be in in the cbind section: =>cbind(mtcars, mtcars[-1,])
. You will need aif
clause to catch and a rule to be applied if the number of rows differ.
– Andre Elrico
Nov 19 at 12:21
So you were right. I've added the 'if' function that I'll edit in and then the 'for' loop worked. However, each level seems to have crazy amount of lists (7000+ when I only expect 26 from 26 files). Would you know why is this a case?
– Num
Nov 19 at 13:34
1
In order to get real and useful help, you will have to provide real data with example and desired outcome. Otherwise its impossible.
– Andre Elrico
Nov 19 at 13:35
@AndreElrico I've added the example and adjusted the code
– Num
Nov 19 at 13:52
1
after#Adding each category to the separate level in the list
try usingdataList[[i]] <- list(nData, sData, dData)
instead of your three lines of code.
– Andre Elrico
Nov 19 at 14:03
1
1
well the error seems to be in in the cbind section: =>
cbind(mtcars, mtcars[-1,])
. You will need a if
clause to catch and a rule to be applied if the number of rows differ.– Andre Elrico
Nov 19 at 12:21
well the error seems to be in in the cbind section: =>
cbind(mtcars, mtcars[-1,])
. You will need a if
clause to catch and a rule to be applied if the number of rows differ.– Andre Elrico
Nov 19 at 12:21
So you were right. I've added the 'if' function that I'll edit in and then the 'for' loop worked. However, each level seems to have crazy amount of lists (7000+ when I only expect 26 from 26 files). Would you know why is this a case?
– Num
Nov 19 at 13:34
So you were right. I've added the 'if' function that I'll edit in and then the 'for' loop worked. However, each level seems to have crazy amount of lists (7000+ when I only expect 26 from 26 files). Would you know why is this a case?
– Num
Nov 19 at 13:34
1
1
In order to get real and useful help, you will have to provide real data with example and desired outcome. Otherwise its impossible.
– Andre Elrico
Nov 19 at 13:35
In order to get real and useful help, you will have to provide real data with example and desired outcome. Otherwise its impossible.
– Andre Elrico
Nov 19 at 13:35
@AndreElrico I've added the example and adjusted the code
– Num
Nov 19 at 13:52
@AndreElrico I've added the example and adjusted the code
– Num
Nov 19 at 13:52
1
1
after
#Adding each category to the separate level in the list
try using dataList[[i]] <- list(nData, sData, dData)
instead of your three lines of code.– Andre Elrico
Nov 19 at 14:03
after
#Adding each category to the separate level in the list
try using dataList[[i]] <- list(nData, sData, dData)
instead of your three lines of code.– Andre Elrico
Nov 19 at 14:03
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53374265%2fadjusting-number-of-rows-for-each-element-in-a-list%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
1
well the error seems to be in in the cbind section: =>
cbind(mtcars, mtcars[-1,])
. You will need aif
clause to catch and a rule to be applied if the number of rows differ.– Andre Elrico
Nov 19 at 12:21
So you were right. I've added the 'if' function that I'll edit in and then the 'for' loop worked. However, each level seems to have crazy amount of lists (7000+ when I only expect 26 from 26 files). Would you know why is this a case?
– Num
Nov 19 at 13:34
1
In order to get real and useful help, you will have to provide real data with example and desired outcome. Otherwise its impossible.
– Andre Elrico
Nov 19 at 13:35
@AndreElrico I've added the example and adjusted the code
– Num
Nov 19 at 13:52
1
after
#Adding each category to the separate level in the list
try usingdataList[[i]] <- list(nData, sData, dData)
instead of your three lines of code.– Andre Elrico
Nov 19 at 14:03