converting a list of lists in a single list - r

A = data.frame( a = c(1:10), b = c(11:20) )
B = data.frame( a = c(101:110), b = c(111:120) )
C = data.frame( a = c(5:8), b = c(55:58) )
L = list(list(B,C),list(A),list(C,A),list(A,B,C),list(C))
I have a list of lists of Data Frames, L but I have to creat a single List of all the Data Frames as below (The ordering of the dataframes should remain same in L and New L.)
NewL = list( B,C,A,C,A,A,B,C,C )

Try reading the manual ;) and
unlist(L,recursive=F)

Related

Conditional statement: change one variable in a data list based on certain input

Can I use conditional statement to change one variable in a data list based on certain input?
For instance, a data list as follows. I need d = perd or phyd when I use different input: dlist[x], d=perd; dlist[y], d=phyd. x and y can be anything, what I need is just to give an order and then make it as perd or phyd.
dlist <- list(
Nsubjects = 1,
Ntrials = 2,
d = perd,
)
perd <- c (1,2,3)
phyd <- c (4,5,6)
Can you create another list with names to store perd and phyd ?
plist <- list(x = c (1,2,3), y = c (4,5,6))
You can then extract the data from it by it's name.
val <- 'x'
dlist <- list(
Nsubjects = 1,
Ntrials = 2,
d = plist[[val]]
)
Without creating plist you can do. :
list(
Nsubjects = 1,
Ntrials = 2,
d = if(val == 1) c(1,2,3) else c(4,5,6)
)
Or also :
list(
Nsubjects = 1,
Ntrials = 2,
d = list(c(1,2,3),c(4,5,6))[[val]]
)
where val <- 1 or 2.

Subsetting everything but a given index in a list (R)

Suppose I have a list of data frames. I am iterating through the list and removing one item (aka one data frame) of the list, and then rbinding the remaining items (aka data frames) of the list to create one final dataframe.
Can you help me how to remove a given index from a list and keep the rest?
Thanks!!! Example code below
testDF1 = data.frame(a = c(1,2,3,4,5), b = c(10,20,30,40,50))
testDF2 = data.frame(a = c(11,12,13,14,15), b = c(110,120,130,140,150))
testDF3 = data.frame(a = c(21,22,23,24,25), b = c(210,220,230,240,250))
testDF4 = data.frame(a = c(31,32,33,34,35), b = c(310,320,330,340,350))
testDF5 = data.frame(a = c(41,42,43,44,45), b = c(410,420,430,440,450))
myList = list(DF1 = testDF1, DF2 = testDF2, DF3 = testDF3, DF4 = testDF4, DF5 = testDF5)
for (i in 1:length(myList)) {
chosenItem = myList[[i]]
removedItemList = myList - chosenItem ## HELP HERE!!!!
updatedList = do.call("rbind", removedItemList)
}
I just figured it out...
for (i in 1:length(myList)) {
chosenItem = myList[[i]]
removedItemList = myList[i]
updatedList = do.call("rbind", removedItemList)
}

Creating a t-test loop over a dataframe using an index

So, let's say I have a 1000-row, 6-column dataframe, the columns are a1, a2, b1, b2, c1, c2. I want to run some t-tests using a's, b's, and c's and get an output df with 3 columns for the t-values of a-b-c and another three for the significance information for those values, making it a total of 6 columns. The problem I have is with rows, I want to loop over chunks of 20, rendering the output a (1000/20=)50-row, 6-column df.
I have already tried creating an index column for my inital df which repeats a 1 for the first 20 row, a 2 for the next 20 row and so on.
convert_n <- function(df) {
df <- df %T>% {.$n_for_t_tests = rep(c(1:(nrow(df)/20)), each = 20)}
}
df <- convert_n(df)
However, I can't seem to find a way to properly utilize the items in this column as indices for a "for" or any kind of loop.
Below you can see the relevant code for that creates a 1-row, 6-column df; I need to modify the [0:20] parts, create a loop that does this for 20 groups and binds them.
t_test_a <- t.test(df$a1[0:20], dfff$a2[0:20], paired = T, conf.level
= 0.95)
t_test_b <- t.test(df$b1[0:20], dfff$b2[0:20], paired = T, conf.level
= 0.95)
t_test_c <- t.test(df$c1[0:20], dfff$c2[0:20], paired = T, conf.level
= 0.95)
t_tests_df <- data.frame(t_a = t_test_a$statistic[["t"]],
t_b = t_test_b$statistic[["t"]],
t_c = t_test_c$statistic[["t"]])
t_tests_df <- t_tests_df %T>% {.$dif_significance_a = ifelse(.$t_a >
2, "YES", "NO")} %T>%
{.$dif_significance_b = ifelse(.$t_b >
2, "YES", "NO")} %T>%
{.$dif_significance_c = ifelse(.$t_c >
2, "YES", "NO")} %>%
dplyr::select(t_a, dif_significance_a,
t_b, dif_significance_b,
t_c, dif_significance_c)
Thank you in advance for your help.
You can use split() and sapply():
set.seed(42)
df <- data.frame(a1 = sample(1000, 1000), a2 = sample(1000, 1000),
b1 = sample(1000, 1000), b2 = sample(1000, 1000),
c1 = sample(1000, 1000), c2 = sample(1000, 1000))
group <- gl(50, 20)
D <- split(df, group)
myt <- function(Di)
with(Di, c(at=t.test(a1, a2)$statistic, ap=t.test(a1, a2)$p.value,
bt=t.test(b1, b2)$statistic, bp=t.test(b1, b2)$p.value,
ct=t.test(c1, c2)$statistic, cp=t.test(c1, c2)$p.value))
sapply(D, FUN=myt) ### or
t(sapply(D, FUN=myt))
This is not the most pretty but i did a for loop like this:
df <- data.frame(a1 = sample(1000, 1000),
a2 = sample(1000, 1000),
b1 = sample(1000, 1000),
b2 = sample(1000, 1000),
c1 = sample(1000, 1000),
c2 = sample(1000, 1000))
df_ttest <- data.frame(p_a = c(1:50),
t_a = c(1:50),
p_b = c(1:50),
t_b = c(1:50),
p_c = c(1:50),
t_c = c(1:50))
index <- 0:50*20
for(i in seq_along(index)) {
df_ttest$p_a[i] = t.test(df$a1[index[i] : index[i+1]])$p.value
df_ttest$p_b[i] = t.test(df$b1[index[i] : index[i+1]])$p.value
df_ttest$p_c[i] = t.test(df$c1[index[i] : index[i+1]])$p.value
df_ttest$t_a[i] = t.test(df$a1[index[i] : index[i+1]])$statistic
df_ttest$t_b[i] = t.test(df$b1[index[i] : index[i+1]])$statistic
df_ttest$t_c[i] = t.test(df$c1[index[i] : index[i+1]])$statistic
}
This gives a 50x6 dataframe with seperate columns of p and t values for every 20 row chunk of a, b and c.
You could even go further and make a nested for loop to cycle through each row in df_ttest to make this abit prettier.

Populating a Data Frame with Characters in a For Loop R

Currently I have a loop that is adding rows from one data frame into another master data frame. Unfortunately, it converts the characters into numbers, but I don't want that. How can I get the following for loop to add the rows from one data frame into the master data frame while keeping the characters?
AnnotationsD <- data.frame(x = vector(mode = "numeric",
length = length(x)), type = 0, label = 0, lesion = 0)
x = c(1,2)
for(i in length(x)){
D = data.frame(x = i, type = c("Distance"),
label = c("*"), lesion = c("Wild"))
AnnotationsD[[i,]] <- D[[i]]
}
So what I would like to come out of this is:
x type label lesion
1 1 Distance * Wild
2 2 Distance * Wild
This should work:
x = c(1,2)
AnnotationsD <- data.frame(x = as.character(NA), type = as.character(NA),
label = as.character(NA), lesion = as.character(NA),
stringsAsFactors =F)
for(i in 1:length(x)){
D = c(x = as.character(i), type = as.character("Distance"),
label = as.character("*"), lesion = as.character("Wild"))
AnnotationsD[i,] <- D
}

R: Get index names while looping through df elements

Say, I have a data frame and I need to do something with its cells and remember what cells I have changed. One way is to loop through indices with two for-loops. But is there a way to do this with one loop?
Perfectly I need something like this:
changes = data.frame(Row = character(), Col = character())
for (cell in df){
if (!(is.na(df))){
cell = do.smt(cell)
temp = list(Row = get.row(cell), Col = get.col(cell))
changes = rbind(changes,temp)
}
}
Example of what I need:
df = data.frame(A = c(1,2,3), B = c(4,5,6), C = c(7,8,9))
rownames(df) = c('a','b','c')
changes = data.frame(Row = NA, Col = NA)
for (i in rownames(df)){
for (j in colnames(df)) {
if (df[i,j] > 5) {
df[i,j] = 0
temp = list(Row = i, Col = j)
changes = rbind(changes, temp)
}
}
}
This gets rid of both loops
df = data.frame(A = c(1,2,3), B = c(4,5,6), C = c(7,8,9))
rownames(df) = c('a','b','c')
changes <- which(df > 5, arr.ind=TRUE)
df[changes] <- 0
If you want the format exactly as specified you can sort that out with
changes <- data.frame(changes,row.names=NULL)
changes$row <- rownames(df)[changes$row]
changes$col <- colnames(df)[changes$col]
and its a simple matter of sorting if you're concerned that the order of the rows matches your example output

Resources