If/else statement using column names in R [closed] - r

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 2 years ago.
Improve this question
I want to do an ifelse function here pulling from an existing df "Tokens" a column name called "Vowel" to create column name "Ambiguity".
If column "Vowel" contains "o" or "u", I want to create a column called "High.Ambiguity", and put the value "1"; else, put "0".
What would the syntax for this look like?

I believe this should do the trick for you. mutate creates a new column, in this case called High.Ambiguity which takes on the value 1 when Vowel (a column in Tokens) is either 'o' or 'u' otherwise it is 0.
library(dplyr)
Tokens <- Tokens %>%
mutate(High.Ambiguity = ifelse(Vowel %in% c("o", "u"), 1, 0))

Related

Retrieve a set of string with unique substring [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 2 years ago.
Improve this question
I have a set of strings in R. In the form of: "X-Y-Z.3000.F.PP0016-C.A-SL-0433.P-N.fC-G.txt". I want to retrieve the set of strings containing just the first occurrence of a string. It depends on the 4th field. In this set for e.g. I have multiple string with X-Y-Z.3000....." I want only the first one having id = 3000, the same for the others.
For reproducibility:
X-Y-Z.3000.F.PP0016-C.A-SL-0433.P-N.fC-G.txt
X-Y-Z.3000.F.PP0016-C.A-SL-0433.F-N.fC-G.txt
X-Y-Z.3008.F.PP0016-C.A-SL-0433.P-N.fC-G.txt
X-Y-Z.3008.F.PP0016-C.B-SX-0433.P-N.fC-G.txt
So at the end I would only the first anche 3th string
X-Y-Z.3000.F.PP0016-C.A-SL-0433.P-N.fC-G.txt
X-Y-Z.3008.F.PP0016-C.A-SL-0433.P-N.fC-G.txt
Extract "4th field" which is 2nd field if we split on ".", then exclude duplicated items:
# data
x <- c("X-Y-Z.3000.F.PP0016-C.A-SL-0433.P-N.fC-G.txt",
"X-Y-Z.3000.F.PP0016-C.A-SL-0433.F-N.fC-G.txt",
"X-Y-Z.3008.F.PP0016-C.A-SL-0433.P-N.fC-G.txt",
"X-Y-Z.3008.F.PP0016-C.B-SX-0433.P-N.fC-G.txt")
x[ !duplicated(sapply(strsplit(x, ".", fixed = "TRUE"), "[", 2)) ]
# [1] "X-Y-Z.3000.F.PP0016-C.A-SL-0433.P-N.fC-G.txt"
# [2] "X-Y-Z.3008.F.PP0016-C.A-SL-0433.P-N.fC-G.txt"

How to split a list in R? [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 2 years ago.
Improve this question
I have an R list where all of the values are in the first position (i.e. list[1]), while I want all the values to be spread evenly throughout the list (list[1] contains one value, list[2] contains the next, etc.). I have been trying unsuccessfully for a while to split the values one position into separate values (each value is a string of characters separated by spaces) but nothing has worked.
Below is an illustration of the sort of situation I am in.
Say "test" is the name of a list in R. Test is an object of length 1, and if you enter test[1] in the console, the output is thousands of values formatted like so:
[1] "value1" "value2" "value3" ... etc.
Now I want to somehow split the contents of list[1] so that each separated character string is in a separate position, so test[1] is "value1", test[2] is "value2", etc. I have looked around for and attempted many purported solutions to this sort of issue (recent example here: List to integer or double in R) but nothing has worked for me so far.
Here's a simple way:
l1 <- list(l1 = round(rnorm(100, 0, 5), 0))
v <- unlist(l1)
l2 <- as.list(v)
length of l1 is 1 and length of l2 is 100. Is this what you are after?

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!

Practice Exercise on tidyr Functions [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 3 years ago.
Improve this question
Using the first.df data frame, separate the DoB column data into 3 new columns - date, month,year by using the separate() function.I tried last line but it is not giving desired result.
fname <- c("Martina", "Monica", "Stan", "Oscar")
lname <- c("Welch", "Sobers", "Griffith", "Williams")
DoB <- c("1-Oct-1980", "2-Nov-1982", "13-Dec-1979", "27-Jan-1988")
first.df <- data.frame(fname,lname,DoB)
print(first.df)
separate(first.df,DoB,c('date','month','year'),sep = '-')
Moved my comment to an actual answer.
To retain the date column you need to add the remove = FALSE parameter, and to discard one of the separated columns simply add NA instead of a column name. The correct command is then
separate(first.df,DoB,c(NA,'month','year'),sep = '-', remove=FALSE)

Replacing values in R conditionally [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 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]

Resources