Splitting vector in dataframe into 2 vectors [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 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)

Related

Splitting one big dataframe into multiple CSV.files [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
Firstly, I have a big data.frame which has 104 rows and 12 columns, I would like to split it up to 13 rows of 8 rows each with the 12 columns.
I am trying to make a code robust enough to not care how many rows there are but simple make a new data.frame every 8 rows.
Also, is it possible after this point to make a code which loops through the 13 data.frames for some calculations?
Here is a way using data.table.split
library(data.table)
#sample data
set.seed(123)
AA <- data.frame( data = rnorm(104) )
#set number of rows to split on
chunksize = 8
#split on create rowid's
l <- split( setDT(AA)[, rowID := (.I-1) %/% chunksize][], by = "rowID")
#names of the list will become the names of the data.frames
names(l) <- paste0( "df", names(l) )
#write the elements of the list to the global environment, using their names
list2env( l, envir = globalenv() )

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)

Extract value from data frame? [closed]

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?

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

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