Want to add characters to every element of a data frame [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 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="_"))

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!

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)

Scale only certain columns 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 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(.))))

Subset rows from list of dataframe [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 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)

Extracting number from a non-consistent string 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 8 years ago.
Improve this question
I have my data as below
Idle|Idle|Idle|Idle|Idle|Idle|Idle
Idle|56|55|49|50|53|48|54|52|Idle|Idle|Idle|Idle|Idle|Idle
Idle|49|51|48|50|50|49|50|57|56|57|56|Idle|Idle|69|86|65|Idle|Idle|Idle|Idle
I want to extract numbers in between(which is phone number in ASCII format) which is
(56|55|49|50|53|48|54|52 for 2nd line and 49|51|48|50|50|49|50|57|56|57|56 for 3rd line),
convert them to numbers between "0 and 9" and concatenate as string/number in new column as phone_number in same data set.
2nd row of new column should be 871230652 and 3rd row should be 13022129898
In ASCII format 48 represents 0 and 57 represents 9
Please help
Thanks,
Here's an approach with regular expressions:
res <- sapply(regmatches(x, gregexpr("^(?:Idle\\|)*\\K\\d+(?=\\|)|\\G(?!^)\\|\\K\\d+",
x, perl = TRUE)),
function(x) paste(as.integer(x) - 48, collapse = ""))
# [1] "" "87125064" "13022129898"
If you want to exclude the empty strings, you can use the following command:
res[as.logical(nchar(res))]
# [1] "87125064" "13022129898"
Here x is this vector:
x <- c("Idle|Idle|Idle|Idle|Idle|Idle|Idle",
"Idle|56|55|49|50|53|48|54|52|Idle|Idle|Idle|Idle|Idle|Idle",
"Idle|49|51|48|50|50|49|50|57|56|57|56|Idle|Idle|69|86|65|Idle|Idle|Idle|Idle")

Resources