Separate Comma Delimited Cells To New Rows
Tag : r , By : user105769
Date : March 29 2020, 07:55 AM
With these it helps Here's an approach that should work for you. I'm assuming that your three input vectors are in different objects. We are going to create a list of those inputs and write a function that process each object and returns them in the form of a data.frame with plyr. The things to take note of here are the splitting of the character vector into it's component parts, then using as.numeric to convert the numbers from the character form when they were split. Since R fills matrices by column, we define a 2 column matrix and let R fill the values for us. We then retrieve the Name column and put it all together in a data.frame. plyr is nice enough to process the list and convert it into a data.frame for us automatically. library(plyr)
a <- paste("A",1, 2,3,4,5,6, sep = ",", collapse = "")
b <- paste("B",1, 2,4,5, sep = ",", collapse = "")
c <- paste("C",1, 2,3,4,6,7,8,9, sep = ",", collapse = "")
input <- list(a,b,c)
splitter <- function(x) {
x <- unlist(strsplit(x, ","))
out <- data.frame(x[1], matrix(as.numeric(x[-1]), ncol = 2))
colnames(out) <- c("Name", "Start", "End")
return(out)
}
ldply(input, splitter)
> ldply(input, splitter)
Name Start End
1 A 1 4
2 A 2 5
3 A 3 6
4 B 1 4
5 B 2 5
6 C 1 6
7 C 2 7
8 C 3 8
9 C 4 9
|
How to merge more than 1 cell value with a comma in a single cell of a row with some restrictions
Date : March 29 2020, 07:55 AM
seems to work fine Here you go! Complete script, just make sure to test really well for every possible case and don't forget to mark it as an answer if it fits your needs: IF OBJECT_ID('TEST1') IS NOT NULL
DROP TABLE TEST1
GO
CREATE TABLE TEST1
(
STUDENTID INT ,
ATTCODE VARCHAR(2) ,
ATTDATE DATE
)
GO
INSERT INTO TEST1
VALUES
('101','AE','20110101'),
('101','H','20110102'),
('101','T','20110105'),
('101','H','20110107'),
('101','AU','20110108'),
('102','AE','20110101'),
('102','AE','20110103'),
('102','H','20110104'),
('102','V','20110105')
GO
IF OBJECT_ID('TEST2') IS NOT NULL
DROP TABLE TEST2
GO
CREATE TABLE TEST2
(
STUDENTID INT,
FROMDATE DATE,
TODATE DATE,
CONSECUTIVEABS INT,
ATTCODES VARCHAR(255)
)
GO
INSERT INTO TEST2
SELECT TEST1.STUDENTID,TEST1.ATTDATE,TEST1.ATTDATE,1,TEST1.ATTCODE
FROM TEST1
JOIN
(
SELECT STUDENTID,MIN(ATTDATE) ATTDATE
FROM TEST1
GROUP BY STUDENTID
) MI ON MI.STUDENTID=TEST1.STUDENTID AND MI.ATTDATE=TEST1.ATTDATE
DECLARE @STUDENTID INT, @ATTCODE VARCHAR(2), @ATTDATE DATE
DECLARE CRS CURSOR LOCAL FAST_FORWARD FOR
SELECT TEST1.STUDENTID,TEST1.ATTCODE,TEST1.ATTDATE
FROM TEST1
LEFT JOIN TEST2 ON TEST1.STUDENTID=TEST2.STUDENTID AND TEST1.ATTCODE=TEST2.ATTCODES AND TEST1.ATTDATE=TEST2.FROMDATE
WHERE TEST2.STUDENTID IS NULL
ORDER BY 1,3 ASC
OPEN CRS
FETCH NEXT FROM CRS INTO @STUDENTID, @ATTCODE, @ATTDATE
WHILE @@FETCH_STATUS=0
BEGIN
IF EXISTS
(
SELECT 1
FROM TEST2
WHERE STUDENTID=@STUDENTID AND DATEADD(DD,1,TODATE)=@ATTDATE
)
UPDATE TEST2
SET TODATE=@ATTDATE, ATTCODES+=','+@ATTCODE, CONSECUTIVEABS+=1
WHERE STUDENTID=@STUDENTID AND DATEADD(DD,1,TODATE)=@ATTDATE
ELSE
INSERT INTO TEST2
SELECT @STUDENTID,@ATTDATE,@ATTDATE,1,@ATTCODE
FETCH NEXT FROM CRS INTO @STUDENTID, @ATTCODE, @ATTDATE
END
SELECT * FROM TEST2
ORDER BY 1,2
|
Excel - Separate Comma List Into Cells Using a Formula
Tag : excel , By : NewGirl
Date : March 29 2020, 07:55 AM
I wish this help you With shoes, hats, umbrellas, towels, disks in A2, put this into a column to the right. =TRIM(MID(SUBSTITUTE($A2, ",", REPT(" ", 999)), (COLUMN(A:A)-1)*999+1, 999))
|
How to merge two cells, add a space and a comma
Tag : excel , By : Cowtung
Date : March 29 2020, 07:55 AM
I hope this helps . I have two cells: , Assuming Cell 1 is A1 and Cell 2 is B1, please try: =A1&", "&B1
=CONCATENATE(A1,", ",B1)
|
How to merge two same column datatable cells in one cell with comma separated in C#?
Date : March 29 2020, 07:55 AM
it should still fix some issue I have tow datatable with the same column like:- , Please try the following code snippet which might help you. DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] { new DataColumn(), new DataColumn(), new DataColumn() });
dt2.Columns.AddRange(new DataColumn[] { new DataColumn(), new DataColumn(), new DataColumn() });
dt3.Columns.AddRange(new DataColumn[] { new DataColumn(), new DataColumn(), new DataColumn() });
Random rnd = new Random();
int count = rnd.Next(3, 10);
for(int i =0; i < count; i++)
{
dt1.Rows.Add("T1C1R" + i, "T1C2R" + i, "T1C3R" + i);
}
count = rnd.Next(3, 10);
for(int i = 0; i < count; i++)
{
dt2.Rows.Add("T2C1R" + i, "T2C2R" + i, "T2C3R" + i);
}
DataTable tmpDt1 = dt1.Rows.Count >= dt2.Rows.Count ? dt1 : dt2;
DataTable tmpDt2 = tmpDt1 == dt1 ? dt2 : dt1;
for(int i = 0; i < tmpDt1.Rows.Count; i++)
{
DataRow dr = dt3.NewRow();
dr.ItemArray = tmpDt1.Rows[i].ItemArray;
if (i < tmpDt2.Rows.Count)
{
dr[0] = dr[0] + ", " + tmpDt2.Rows[i][0];
dr[1] = dr[1] + ", " + tmpDt2.Rows[i][1];
dr[2] = dr[2] + ", " + tmpDt2.Rows[i][2];
}
dt3.Rows.Add(dr);
}
dataGridView1.DataSource = dt1;
dataGridView2.DataSource = dt2;
dataGridView3.DataSource = dt3;
|