Extract value from data frame? [closed] - r

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
If I have a data frame, the standard approach to extracting a value is to use a Boolean/logical expression to match the correct row and column. Example:
set.seed(1)
df <- data.frame(letters = letters[1:3], numbers = as.character(c(1, 2, 1)), value=rnorm(3))
subset(df, letters=="c" & numbers=="1")$value
[1] -0.8356286
However, stringing together many == statements seems a bit kludgey. Another way would be to use row names as keys:
Key <- function(...) paste(..., sep="%") # this could be any formatting
row.names(df) <- with(df, Key(letters, numbers))
df[Key("c", "1"), "value"]
[1] -0.8356286
In the tidyverse, use of row names is discouraged - what would be the recommended way to match and extract values?

Related

How to extract any given number from a dataframe and assign it a name [closed]

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!

Question about common values across columns [closed]

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 4 years ago.
Improve this question
I have a set of 20 column, each contains number value. I would like to have a function in excel or in r or somewhere else to extract the shared values among all the columns.
Several of the online Venn tools can visualize and list among up to 6 columns.
Any tool?
Thanks
in R, we can use intersect with Reduce to get the common values across all the columns
Reduce(intersect, dftest)
data
dftest <- data.frame(col1 = 1:5, col2 = 2:6, col3 = 3:7)

Function Ranking in R [closed]

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 4 years ago.
Improve this question
I have two columns x and y. I want to have one column which contains the ranking for both columns. I thought about sum both column and then get it ranked, does any one have a function that rank two columns in r?
Many thanks
If you are just wanting to use the rank function as you suggest:
df1 <- data.frame(x = rnorm(10), y = rnorm(10))
apply(df1, 2, rank) # 2 columns with separate rankings
rank(rowSums(df1)) # sum by rows first, then rank
rank(rowMeans(df1)) # avg by rows first, then rank (same result!)

Splitting vector in dataframe into 2 vectors [closed]

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 6 years ago.
Improve this question
I need to split a vector in a dataframe(last.first) into 2 separate vectors (firstname, lastname) and then put the 2 vectors back into the dataframe. What should I do.
You can split names with strsplit, use whatever is separating the first and second names instead of " " (space in my example).
This will give you a list. Which can be made into dataframe via ldply or unlist to matrix
person.names <- c("Adam Smith", "Max Webber")
temp.list <- strsplit(person.names, " ")
names.df <- ldply(temp.list, function (x) data.frame(first = x[1], second = x[2]))
first second
1 Adam Smith
2 Max Webber
or
matrix(unlist(temp.list), ncol = 2, byrow = TRUE)

Want to add characters to every element of a data frame [closed]

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="_"))

Resources