Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to subset rows,say from 130:150, in each dataframe present in a list. I have written a below code to subset:
test<-lapply(res,subset, [130:150,]) # res contains the list of dataframes
But this code is throwing below error:
Error in res[130:150, ] : incorrect number of dimensions
Thanks in advance!
res <- list(mtcars,mtcars)
lapply(res, function(x) return(x[2:4,]))
is returning the rows 2 to 4 of each dataframe. If you want the columns, use
lapply(res, function(x) return(x[,2:4]))
or Gregors solution lapply(res, "[", 2:4)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How do I extract a number in any given location of a dataframe? Let's say I have a 4x4 matrix, how would I take the number value in (2,4) and assign that value a name?
You can use the setNames function as so: setNames(value, c(name1))
This works for vectors and columns too- for instance: setNames(df[c(col1, col2), c(name1, name2)]; and setNames(c(val1, val2, val3), c(name1, name2, name3))
Edit-
#dataframe with one row and two columns as such
df <- data.frame('a','b')
#You can access a value by:
val <- levels(droplevels(df[1,2])) #Value at first row, second column
#To assign it a name, you can either use:
setNames(val, c(name))
#or
names(val) <- c(name)
Hope this helps!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to split a list of tibble into ther respective tibbles. How do I do that? I have 245 tibbles in the list so I can't do that by each one.
Create an example dataset:
library(tibble)
myList <- list("iris_tbl" = as_tibble(iris),
"cars_tbl" = as_tibble(cars))
Answer:
mapply(assign, names(myList), myList, MoreArgs=list(envir = globalenv()))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
How can I scale(x) only certain columns of a dataframe? I have a dataframe with 7 columns and I want to scale only column 3 and 6. The rest should stay as it is.
We can do this with lapply. Subset the columns of interest, loop through them with lapply, assign the output back to the subset of data. Here, we are using c because the outpuf of scale is a matrix with a single column. Using c or as.vector, it gets converted to vector
df[c(3,6)] <- lapply(df[c(3, 6), function(x) c(scale(x)))
Or another option is mutate_at from dplyr
library(dplyr)
df %>%
mutate_at(c(3,6), funs(c(scale(.))))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a data frame of strings as below and would like to add the string "Market" to each of the elements of the data frame. Is there a function that would allow me to do this easily without having to use a for loop?
V1
1 PUBLIC_DISPATCHSCADA_20141221.zip
2 PUBLIC_DISPATCHSCADA_20141222.zip
3 PUBLIC_DISPATCHSCADA_20141223.zip
4 PUBLIC_DISPATCHSCADA_20141224.zip
5 PUBLIC_DISPATCHSCADA_20141225.zip
6 PUBLIC_DISPATCHSCADA_20141226.zip
We can use paste and specify the delimiter. In this case, I am using _ and pasteing the "Market" at the beginning of the string.
df1$V1 <- paste("Market", df1$V1, sep="_")
If we need to do this for each column
df1[] <- lapply(df1, function(x) paste("Market", x, sep="_"))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to conditionally replace the values of each columns in R. My data looks like below image.
In this, I want to check if the values are >UCL then want to replace with UCL value and If the values are
The Output should look like below image:
Like this I have many rows and columns data and I'm looking for solution in R.
We create a 'df2' as a copy of 'df1'. Using the 'i1' and 'i2' index, we replace the columns 1:4 in 'df2' with corresponding 'UCL' and 'ICL' values that fits the condition.
df2 <- df1
i1 <- df1[1:4] > df1$UCL
i2 <- df1[1:4] < df1$LCL
df2[1:4][i1] <- df2$UCL[row(df2[1:4])][i1]
df2[1:4][i2] <- df2$LCL[row(df2[1:4])][i2]